我试图更改外键的约束,但我一直收到此错误:
sqlalchemy.exc.OperationalError:(OperationalError)(1025,"将' ./ tillsammans_dev / profile_accommodation_contact'重新命名为' ./ tillsammans_dev /#sql2-4c1-37& #39;(错误号: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")
区别在于:
编辑: 所以我的问题是我应该如何编辑迁移文件,以便将此更改迁移到模型中? 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 ###