Skip to content

Foreign key creation fails if table has already a foreign key to referenced table #254

Closed
@sqlalchemy-bot

Description

@sqlalchemy-bot

Migrated issue, originally created by Lucas Kahlert (@f3anor)

If you create add a column incl. a foreign key with a batch operation, e.g.

with op.batch_alter_table('quest') as batch_op:
    batch_op.add_column(sa.Column('therapist_id', sa.String(255), nullable=True))
    batch_op.create_foreign_key('fk_quest_therapist_id_user', 'user', ['therapist_id'], ['username'])

but the referenced table (user) was already created, because there is another column in quest referring to user, the upgrade command will produce the follwing error:

InvalidRequestError: Table 'user' is already defined for this MetaData instance.  Specify 'extend_existing=True' to redefine options and columns on an existing Table object.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions