1

我正在尝试更改外键的约束,但我不断收到此错误:

sqlalchemy.exc.OperationalError:(OperationalError)(1025,“将'./tillsammans_dev/profile_accommodation_contact'重命名为'./tillsammans_dev/#sql2-4c1-37'时出错(错误号:152)”)'ALTER TABLE profile_accommodation_contact DROP FOREIGN KEY profile_accommodation_contact_ibfk_1' ()

我以前遇到过这个问题,但是每当我遇到这个错误时,我都会擦除整个数据库并重建它,但我想我会尝试学习如何进行正确的迁移。

我更新前的模型:

class ProfileAccommodationContact(CRUDMixin, db.Model):
  __tablename__ = 'profile_accommodation_contact'

  show_contact_info = db.Column(db.Boolean, nullable=True, default=False)
  message = db.Column(db.String(254), nullable=True)
  created = db.Column(db.BigInteger, nullable=False, default=time.time())
  seen = db.Column(db.Boolean, nullable=True, default=False)

  fk_from_user = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)
  r_from_user = db.relationship('User', backref='ProfileAccommodationContact', uselist=False, cascade='all', primaryjoin='User.id==ProfileAccommodationContact.fk_from_user')

  fk_to_user = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=True)
  r_to_user = db.relationship('User', uselist=False, cascade='all', primaryjoin='User.id==ProfileAccommodationContact.fk_to_user')

  fk_profile_accommodation = db.Column(db.Integer, db.ForeignKey('profile_accommodation.id'), nullable=False)
  r_profile_accommodation = db.relationship('ProfileAccommodation', backref='ProfileAccommodationContact', uselist=False, cascade='all')

  fk_contact_type = db.Column(db.Integer, db.ForeignKey('contact_type.id'), nullable=False)
  r_contact_type = db.relationship('ContactType', backref='ProfileAccommodationContact', uselist=False, cascade="all")

我希望我的模型在数据库中的样子:

class ProfileAccommodationContact(CRUDMixin, db.Model):
  __tablename__ = 'profile_accommodation_contact'

  show_contact_info = db.Column(db.Boolean, nullable=True, default=False)
  message = db.Column(db.String(254), nullable=True)
  created = db.Column(db.BigInteger, nullable=False, default=time.time())
  seen = db.Column(db.Boolean, nullable=True, default=False)

  fk_from_user = db.Column(db.Integer, db.ForeignKey('user.uid'), nullable=True)
  r_from_user = db.relationship('User', backref='ProfileAccommodationContact', uselist=False, cascade='all', primaryjoin='User.uid==ProfileAccommodationContact.fk_from_user')

  fk_to_user = db.Column(db.Integer, db.ForeignKey('user.uid'), nullable=True)
  r_to_user = db.relationship('User', uselist=False, cascade='all', primaryjoin='User.uid==ProfileAccommodationContact.fk_to_user')

  fk_profile_accommodation = db.Column(db.Integer, db.ForeignKey('profile_accommodation.id'), nullable=False)
  r_profile_accommodation = db.relationship('ProfileAccommodation', backref='ProfileAccommodationContact', uselist=False, cascade='all')

  fk_contact_type = db.Column(db.Integer, db.ForeignKey('contact_type.id'), nullable=False)
  r_contact_type = db.relationship('ContactType', backref='ProfileAccommodationContact', uselist=False, cascade="all")

区别在于:

  • fk_from_user
  • r_from_user
  • fk_to_user
  • r_to_user

编辑:所以我的问题是我应该如何编辑迁移文件,以便我可以将此更改迁移到模型?flask-migration 创建的文件如下所示(在第一个op.drop_constraint上失败):

def upgrade():
  ### commands auto generated by Alembic - please adjust! ###
  op.drop_constraint(u'profile_accommodation_contact_ibfk_1', 'profile_accommodation_contact', type_='foreignkey')
  op.drop_constraint(u'profile_accommodation_contact_ibfk_2', 'profile_accommodation_contact', type_='foreignkey')
  op.create_foreign_key(None, 'profile_accommodation_contact', 'user', ['fk_to_user'], ['uid'])
  op.create_foreign_key(None, 'profile_accommodation_contact', 'user', ['fk_from_user'], ['uid'])
  ### end Alembic commands ###

def downgrade():
  ### commands auto generated by Alembic - please adjust! ###
  op.drop_constraint(None, 'profile_accommodation_contact', type_='foreignkey')
  op.drop_constraint(None, 'profile_accommodation_contact', type_='foreignkey')
  ### end Alembic commands ###
4

0 回答 0