Skip to content
Advertisement

How create Postgres database using SQLAlchemy

I want write project using database library (SQLAlchemy). When I use SQLite, all works good, but when I deploy project on server (Heroku with Postgres plugin), It doesn’t work. This is code for my database:

class UserBase(Base):
    __tablename__ = 'users'

    id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
    nickname = Column(String(50), unique=True, nullable=False)
    server = Column(Integer)
    language = Column(String(2))

    def __repr__(self):
        return "<UserBase(nickname='%s', server='%s', language='%s')>" % 
            (self.nickname, self.server, self.language)

I have engine & Base variable:

engine = create_engine(os.environ.get('DATABASE_URL'))
Base = declarative_base()

In DATABASE_URL link to base on Heroku(Postgres). For create database I write in Interactive Python Console:

from user_base import Base, engine
Base.metadata.create_all(engine)

After these manipulations in the server logs it gives this:

sqlalchemy.exc.DataError: (psycopg2.errors.NumericValueOutOfRange) integer out of range

How to fix problem? If you needs in info, asking.

Advertisement

Answer

I set type of server as String, indicated that the server is a string and this is helped me.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement