Tasteam
Tasteam

Reputation: 143

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

Upvotes: 14

Views: 8686

Answers (1)

davidism
davidism

Reputation: 127370

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.

Upvotes: 37

Related Questions