简体   繁体   English

sqlalchemy.exc.NoReferencedTableError:外键找不到表

[英]sqlalchemy.exc.NoReferencedTableError: Foreign key could not find table

I have some models that i'd like to migrate, 2 of them are:我有一些我想迁移的模型,其中 2 个是:

FamilyMember.py家庭成员.py

class FamilyMember(db.Model):
    __tablename__ = 'family_members'
    id = db.Column(db.Integer, primary_key=True)
    parent_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    name = db.Column(db.String(120), index=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password = db.Column(db.String(128), nullable=False)
    notification = db.Column(db.Boolean, default=True)
    auto_ml = db.Column(db.Boolean, default=True)
    photo = db.Column(db.String(128), nullable=True)
    created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
    updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=db.func.now())
    notifications = db.relationship('Notification', backref='family_members', lazy='dynamic')

And Notification.py和 Notification.py

class Notification(db.Model):
    __tablename__ = 'notifications'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    fm_id = db.Column(db.Integer, db.ForeignKey('family_members.id'))
    text = db.Column(db.String(255))
    read = db.Column(db.Boolean, default=False)
    type = db.Column(db.Integer)
    created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
    updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=db.func.now())

Regarding to this post , i have to explicitly state table name with __tablename__ = 'tablename' , i've done that but it didn't work the way it supposed to and still got the error sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'notifications.fm_id' could not find table 'family_members' with which to generate a foreign key to target column 'id' .关于这篇文章,我必须使用__tablename__ = 'tablename'明确 state 表名,我已经这样做了,但它没有按预期的方式工作,仍然出现错误sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'notifications.fm_id' could not find table 'family_members' with which to generate a foreign key to target column 'id' What should i do?我应该怎么办?

You can use back_populates instead of backref in db.relationship()您可以在 db.relationship() 中使用back_populates而不是backref

class Notification(db.Model):
    __tablename__ = 'notifications'
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    fm_id = db.Column(db.Integer, db.ForeignKey('family_members.id'))
    text = db.Column(db.String(255))
    read = db.Column(db.Boolean, default=False)
    type = db.Column(db.Integer)
    created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
    updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=db.func.now())
    family_members = db.relationship("FamilyMember", back_populates="notifications")

class FamilyMember(db.Model):
    __tablename__ = 'family_members'
    id = db.Column(db.Integer, primary_key=True)
    parent_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    name = db.Column(db.String(120), index=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password = db.Column(db.String(128), nullable=False)
    notification = db.Column(db.Boolean, default=True)
    auto_ml = db.Column(db.Boolean, default=True)
    photo = db.Column(db.String(128), nullable=True)
    created_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now())
    updated_at = db.Column(db.DateTime(timezone=True), server_default=db.func.now(), onupdate=db.func.now())
    notifications = db.relationship("Notification", back_populates="family_member")
    

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 sqlalchemy.exc.NoReferencedTableError:与列 X 关联的外键找不到用于生成外键的表 Y - sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column X could not find table Y with which to generate a foreign key Sqlalchemy.exc.NoReferencedTableError - Sqlalchemy.exc.NoReferencedTableError Sqlalchemy 错误:sqlalchemy.exc.NoReferencedTableError - Sqlalchemy Error: sqlalchemy.exc.NoReferencedTableError 如何摆脱 sqlalchemy.exc.NoReferencedTableError? - How to get rid of sqlalchemy.exc.NoReferencedTableError? Sql alchemy sqlalchemy.exc.NoReferencedTableError 不创建表 - Sql alchemy sqlalchemy.exc.NoReferencedTableError not creating tables sqlalchemy 外键找不到表 - sqlalchemy foreign key could not find table Flask SQLAlchemy 找不到用于生成外键的表,即使表存在 - Flask SQLAlchemy could not find table with which to generate Foreign Key even though table exists SQLalchemy 找不到用于创建外键的表 - SQLalchemy not find table for creating foreign key sqlalchemy.exc.ArgumentError: Mapper mapped class could notassemble any primary key columns for mapped table 'houses' - sqlalchemy.exc.ArgumentError: Mapper mapped class could not assemble any primary key columns for mapped table 'houses' sqlalchemy.exc.ArgumentError: Mapper mapped class could notassemble any primary key columns for mapped table'users' - sqlalchemy.exc.ArgumentError: Mapper mapped class could not assemble any primary key columns for mapped table 'users'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM