Flask-migrate を使用して移行を作成しています。私は次のように2つのモデルを持っています -
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)
最初のモデル、つまりユーザーは最初の移行で作成されますが、これは問題ありません。しかし、2 番目のモデル (Post) を追加すると、外部キー制約が無視され、次の移行ファイルが生成されます -
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 ###
user フィールドと body フィールドを無視しているだけです。すべてのフィールドが使用されるようにするにはどうすればよいですか?