Home > database >  Flask migrate error - foreign key associated with column could not find table with which to generate
Flask migrate error - foreign key associated with column could not find table with which to generate

Time:02-11

I have two models as following in different files. When I run flask db migrate I get this error.

raise exc.NoReferencedTableError(
sqlalchemy.exc.NoReferencedTableError: Foreign key associated with column 'datasets.user_id' could not find table 'user' with which to generate a foreign key to target column 'id'

What am i doing wrong? Please help me out, thanks.

class User(Model):
    __tablename__ = "users"
    id = Column(db.Integer, primary_key=True)
    email = Column(db.String(64), unique=True, index=True)
    username = Column(db.String(15), unique=True, index=True)
    role_id = Column(db.Integer, db.ForeignKey("roles.id"))
    userdataset = db.relationship("Dataset", backref="user")

class Dataset(db.Model):
    __tablename__ = 'datasets'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), index=True, unique=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

CodePudding user response:

If you set the value of the foreign key in the dataset table to users.id, it will work.

  • Related