python - SQLAlchemy alembic AmbiguousForeignKeysError for declarative type but not for equivalent non-declarative type -
i have following alembic migration:
revision = '535f7a49839' down_revision = '46c675c68f4' alembic import op import sqlalchemy sa sqlalchemy.ext.declarative import declarative_base sqlalchemy.orm import sessionmaker datetime import datetime session = sessionmaker() base = declarative_base() metadata = sa.metadata() # table definition works organisations = sa.table( 'organisations', metadata, sa.column('id', sa.integer, primary_key=true), sa.column('creator_id', sa.integer), sa.column('creator_staff_member_id', sa.integer), ) """ # doesn't... class organisations(base): __tablename__ = 'organisations' id = sa.column(sa.integer, primary_key=true) creator_id = sa.column(sa.integer) creator_staff_member_id = sa.column(sa.integer) """ def upgrade(): bind = op.get_bind() session = session(bind=bind) session._model_changes = {} # if using flask-sqlalchemy, works around bug print(session.query(organisations).all()) raise exception("don't succeed") def downgrade(): pass
now query session.query(organisations).all()
works when use imperatively-defined table (the 1 not commented out). if use declarative version, far understand should equivalent, error:
sqlalchemy.exc.ambiguousforeignkeyserror: not determine join condition between parent/child tables on relationship staffmember.organisation - there multiple foreign key paths linking tables. specify 'foreign_keys' argument, providing list of columns should counted containing foreign key reference parent table.
now understand error means: have 2 foreign keys organisations
staff_members
in actual models. why alembic care these, , how know exist? how migration know called staffmember
exists? far understand, alembic should know models explicitly tell in migration.
turns out problem flask-script setup using call alembic. command using call alembic importing code initialise flask app importing actual models.
Comments
Post a Comment