I need to update my database by adding one table, and one column to the existing table. The new column and the table should have one-to-many relation.
here is the alembic revision file:
def upgrade():
op.create_table('categories',
sa.Column('category_id', sa.Integer, primary_key=True),
sa.Column('category_name', sa.String(30)),
sa.Relationship('post', backref='cat', lazy='dynamic') )
op.add_column('post', sa.Column('category', sa.Integer, sa.ForeignKey('categories.category_id')) )
The problem is with this line:
sa.Relationship('post', backref='cat', lazy='dynamic') )
What is the correct code to define relation here? Thank You
Relationships are defined only on the SQLAlchemy side, not on the SQL side. Simply create the tables or columns that you need, and the relationship will work correctly. Therefore, it should not be in the migration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With