Skip to content
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 FK comparison (and uniques, indexes?) comparing on column.key, but reflection doesn't have this #260

Closed
sqlalchemy-bot opened this issue Jan 2, 2015 · 2 comments

Comments

@sqlalchemy-bot
Copy link

@sqlalchemy-bot sqlalchemy-bot commented Jan 2, 2015

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

@sqlalchemy-bot sqlalchemy-bot commented Jan 10, 2015

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 #260

d12da7c

@sqlalchemy-bot
Copy link
Author

@sqlalchemy-bot sqlalchemy-bot commented Jan 10, 2015

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
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.