Skip to content

autogenerate FK comparison (and uniques, indexes?) comparing on column.key, but reflection doesn't have this #260

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

Closed
sqlalchemy-bot opened this issue Jan 2, 2015 · 2 comments
Labels
autogenerate - detection bug Something isn't working
Milestone

Comments

@sqlalchemy-bot
Copy link

Migrated issue, originally created by Michael Bayer (@zzzeek)

given local metadata:

Table(
    't1', target_metadata,
    Column('t1_id', Integer, key='id', primary_key=True),
)

Table(
    't2', target_metadata,
    Column('t2_id', Integer, key='id', primary_key=True),
    Column('t2_t1id', Integer, key='t1id'),
    ForeignKeyConstraint(['t1id'], ['t1.id'])
)


the DDL for this is:

CREATE TABLE t1 (
	t1_id SERIAL NOT NULL, 
	t1_foo VARCHAR, 
	PRIMARY KEY (t1_id)
)


CREATE TABLE t2 (
	t2_id SERIAL NOT NULL, 
	t2_t1id INTEGER, 
	PRIMARY KEY (t2_id), 
	FOREIGN KEY(t2_t1id) REFERENCES t1 (t1_id)
)

autogenerate here fails because ddl.base._fk_spec is using column.key for the source columns:

diff --git a/alembic/ddl/base.py b/alembic/ddl/base.py
index d497253..6a5a5fc 100644
--- a/alembic/ddl/base.py
+++ b/alembic/ddl/base.py
@@ -172,10 +172,11 @@ def _columns_for_constraint(constraint):
 
 def _fk_spec(constraint):
     if util.sqla_100:
-        source_columns = constraint.column_keys
+        source_columns = [
+            constraint.parent.c[key].name for key in constraint.column_keys]
     else:
         source_columns = [
-            element.parent.key for element in constraint.elements]
+            element.parent.name for element in constraint.elements]
 
     source_table = constraint.parent.name
     source_schema = constraint.parent.schema

uq and ix seem to be using column.name and might be OK but tests should be added.

@sqlalchemy-bot
Copy link
Author

Michael Bayer (@zzzeek) wrote:

  • Fixed bug in foreign key autogenerate where if the in-Python table
    used custom column keys (e.g. using the key='foo' kwarg to
    Column), the comparison of existing foreign keys to those specified
    in the metadata would fail, as the reflected table would not have
    these keys available which to match up. Foreign key comparison for
    autogenerate now ensures it's looking at the database-side names
    of the columns in all cases; this matches the same functionality
    within unique constraints and indexes.
    fixes autogenerate FK comparison (and uniques, indexes?) comparing on column.key, but reflection doesn't have this #260

d12da7c

@sqlalchemy-bot
Copy link
Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

@sqlalchemy-bot sqlalchemy-bot added autogenerate - detection bug Something isn't working labels Nov 27, 2018
@sqlalchemy-bot sqlalchemy-bot added this to the tier 1 milestone Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
autogenerate - detection bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant