Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alembic: create relationship in revision file

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

like image 372
Tasteam Avatar asked Nov 29 '22 14:11

Tasteam


1 Answers

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.

like image 77
davidism Avatar answered Dec 05 '22 09:12

davidism