I have a SqlAlchemy/Flask application. In it, I have an existing model named MyModelA
. This is what it looks like:
class MyModelA(db.Model):
a_id = db.Column(db.Integer, nullable=False, primary_key=True)
my_field1 = db.Column(db.String(1024), nullable=True)
Now, I am adding a child model MyModelB
. This is what it looks like:
class MyModelB(db.Model):
b_id = db.Column(db.Integer, nullable=False, primary_key=True)
a_id = db.Column(db.Integer, db.ForeignKey(MyModelA.a_id), nullable=False)
my_field2 = db.Column(db.String(1024), nullable=True)
Then I run python manage.py migrate
. This is what shows up in the migration file:
def upgrade():
op.create_table('my_model_b',
sa.Column('b_id', sa.Integer(), nullable=False),
sa.Column('a_id', sa.Integer(), nullable=False),
sa.Column('my_field2', sa.String(length=1024), nullable=True),
sa.ForeignKeyConstraint(['a_id'], [u'my_model_a.a_id'], ),
sa.PrimaryKeyConstraint('b_id')
)
def downgrade():
op.drop_table('my_table_b')
I want to edit this migration such that it for every instance of MyModelA
, a child record of instance MyModelB
should be created with MyModelB.my_field2
set to MyModelA.my_field1
. How can I do it?
Please show the code for upgrade and downgrade.