weixin_39890652 2020-11-30 11:37
浏览 0

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

Migrated issue, originally created by 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/alembic

  • 写回答

8条回答 默认 最新

  • weixin_39890652 2020-11-30 11:37
    关注

    Michael Bayer () 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.

    评论

报告相同问题?