Search code examples
pythonflaskflask-sqlalchemydatabase-migrationflask-migrate

Flask migrate ignoring foreign key


I am using Flask-migrate to create migrations. I have 2 models as follows -

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120), unique=True)
    password_hash = db.Column(db.String(120))
    handle = db.Column(db.String(120))
    type_user = db.Column(db.String(50))
    display_pic = db.Column(db.String(100))

    def __init__(self, handle, email, raw_password):
        self.handle = handle
        self.email = email
        # Save the hashed password
        self.set_password(raw_password)

    def __repr__(self):
        return '<User %r>' % self.username

    def set_password(self, raw_password):
        self.password_hash = generate_password_hash(raw_password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)


class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    likes = db.Column(db.Integer)
    user = db.relationship('User', backref=db.backref('posts'))
    body = db.column(db.Text)

The first model, i.e. User is made in the first migration, which is fine. But when I add the second model(Post), the foreign key constrain is ignored, and the following migration file is generated -

from alembic import op
import sqlalchemy as sa

def upgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.create_table('post',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('likes', sa.Integer(), nullable=True),
    sa.PrimaryKeyConstraint('id')
    )
    ### end Alembic commands ###


def downgrade():
    ### commands auto generated by Alembic - please adjust! ###
    op.drop_table('post')
    ### end Alembic commands ###

It is just ignoring the user and body fields. How can I fix this, so that all the fields are used?


Solution

  • Sorry. my bad, I got confused in the documentation of foreign keys. The following code works perfectly -

    from backend import db
    from werkzeug.security import generate_password_hash, check_password_hash
    
    
    class User(db.Model):
        id = db.Column(db.Integer, primary_key=True, autoincrement=True)
        name = db.Column(db.String(80), unique=True)
        email = db.Column(db.String(120), unique=True)
        password_hash = db.Column(db.String(120))
        handle = db.Column(db.String(120))
        type_user = db.Column(db.String(50))
        display_pic = db.Column(db.String(100))
        posts = db.relationship('Post', backref='user', lazy='dynamic')
    
        def __init__(self, handle, email, raw_password):
            self.handle = handle
            self.email = email
            # Save the hashed password
            self.set_password(raw_password)
    
        def __repr__(self):
            return '<User %r>' % self.username
    
        def set_password(self, raw_password):
            self.password_hash = generate_password_hash(raw_password)
    
        def check_password(self, password):
            return check_password_hash(self.password_hash, password)
    
    
    class Post(db.Model):
        id = db.Column(db.Integer, primary_key=True, autoincrement=True)
        likes = db.Column(db.Integer)
        body = db.Column(db.Text)
        user_id = db.Column(db.Integer, db.ForeignKey('user.id'))