1

I have passed compare_type=True to the constructor of Mirate.

migrate = Migrate(app, db, compare_type=True)

My models are following:

class User(db.Model):
    __tablename__ = 'user'
    id = Column(mysql.BIGINT(20, unsigned=True),
                nullable=False, primary_key=True)
    name = Column(mysql.VARCHAR(50), unique=True, nullable=False)
    email = Column(mysql.VARCHAR(50), unique=True, nullable=False)
    income = db.relationship(Income, backref="user")

class Income(db.Model):
    __tablename__ = 'income'
    id = Column(mysql.BIGINT(20, unsigned=True),
                nullable=False, primary_key=True)
    user_id = Column(mysql.BIGINT(20, unsigned=True), db.ForeignKey('user.id'),
                     nullable=False)
    year = Column(Integer, nullable=False)

Now, flask db init, flask db migrate works perfect. flask db upgrade even creates the tables. but the BIGINT field length is ignored. My first question is why is this happening?

And secondly, when I try to migrate again with flask db migrate, I get following output:

INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected type change from BIGINT(unsigned=True) to BIGINT(display_width=20, unsigned=True) on 'income.id'
INFO  [alembic.autogenerate.compare] Detected type change from BIGINT(unsigned=True) to BIGINT(display_width=20, unsigned=True) on 'income.user_id'
INFO  [alembic.autogenerate.compare] Detected type change from BIGINT(unsigned=True) to BIGINT(display_width=20, unsigned=True) on 'user.id'

This is strange because first time it ignored the length and now it's detecting it as a type change. After that, if I try to upgrade with flask db upgrade, I get the following error:

sqlalchemy.exc.DataError: (MySQLdb._exceptions.DataError) (1171, 'All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead')
[SQL: ALTER TABLE income MODIFY id BIGINT(20) UNSIGNED NULL AUTO_INCREMENT]
(Background on this error at: http://sqlalche.me/e/9h9h)

Now, this should not be happening as I have already used nullable=False in the id field of both User and Income model. Need a quick solution to this problem. Is this because of any bug or there is something wrong with my code?

nsssayom
  • 364
  • 1
  • 5
  • 21

0 Answers0