New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

autogenerate breaks on fk specified on remote name when remote key != name #456

Closed
sqlalchemy-bot opened this Issue Oct 10, 2017 · 8 comments

Comments

Projects
None yet
1 participant
@sqlalchemy-bot

sqlalchemy-bot commented Oct 10, 2017

Migrated issue, originally created by dataradiant (@dataradiant)

I have a foreign key where the remote column is referenced by name and link_to_name=True. When using autogenerate, I receive a KeyError on the column name here: https://bitbucket.org/zzzeek/alembic/src/feaea56b97a11f4f132fb5f55d8a6b71dc0b9c04/alembic/autogenerate/render.py?at=master&fileviewer=file-view-default#render.py-685

It's trying to lookup the column using the string provided, which in this case is the name rather than the key, which obviously doesn't work so well when they aren't the same.

Replacing that line with a conditional that checks for link_to_name seems to work at least for my case:

if fk.link_to_name:
    colname = _ident(colname)
else:
    colname = _ident(parent_metadata.tables[table_fullname].c[colname].name)
@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Oct 10, 2017

Changes by dataradiant (@dataradiant):

  • edited description
@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Oct 11, 2017

Michael Bayer (@zzzeek) wrote:

here's the funniest part of that function:

"""Implement a 'safe' version of ForeignKey._get_colspec() that
never tries to resolve the remote table."""

then

try to resolve the remote table and adjust for column.key

what a letdown.

as for the issue, need a reproducer. Guess this is just the render() operation.

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Oct 11, 2017

Michael Bayer (@zzzeek) wrote:

the two options here would be either to also replicate the "link_to_name=True" flag if we are still linking to name, or to replace the name with the key if we've resolved it.

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Oct 11, 2017

Michael Bayer (@zzzeek) wrote:

nevermind, i recall we are just trying to render for DB-direct operations only

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Oct 11, 2017

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Oct 11, 2017

Changes by Michael Bayer (@zzzeek):

  • added labels: autogenerate - rendering
@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Oct 11, 2017

Michael Bayer (@zzzeek) wrote:

Clean up _fk_colspec() for link_to_name, no column found

A :class:.ForeignKeyConstraint can now render correctly if the
link_to_name flag is set, as it will not attempt to resolve the name
from a "key" in this case. Additionally, the constraint will render
as-is even if the remote column name isn't present on the referenced
remote table.

Change-Id: Ic030fbc106973231b6877c1acfb5349e515335a8
Fixes: #456

0a0524d

@sqlalchemy-bot

This comment has been minimized.

sqlalchemy-bot commented Oct 11, 2017

Changes by Michael Bayer (@zzzeek):

  • changed status to closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment