Dewey Gaedcke <de...@pathoz.com> wrote:

> Is it likely that this construct:
> 
> @event.listens_for(Column, "before_parent_attach")
> def attach(target, cls):
>     # cls is the table obj, not the declarative class
>     target.name = "%s%s" % (cls.name[0:4], target.name)
> 
> is interfering with Alembic??

yes.    the above process will definitely break table reflection which is what 
you are using when you make use of autogenerate.

> 
> I'm getting this stack trace and it changes if I disable the event listener 
> above.
> 
> I don't actually have any columns named  "cat_cat_sit_id" so it looks like 
> that prefix is being double attached???

this is the table that’s being reflected from your database, first of all, so 
you definitely don’t want to be changing .name in that case, because it is set 
to exactly what the name is in the DB.

the order of steps is:

1. reflection loads the name of the column from the DB.  In this case I’m going 
to say it’s “cat_sit_id”.  which makes sense because the scheme you’re going 
for here is <tablename>_<colname>.

2. reflection creates a Column(name) object, name “sit_id”.

3. Column copies the “.name” field to that of “.key”.

4. reflection attaches the Column to the Table, I’m going to guess it’s named 
“cat”.

5. your event fires off.  Renames .name to “cat_cat_sit_id”.   Does not change 
the .key.

6. The column is placed into table.c with the key “sit_id”.  So 
table.c.sit_id.name == “cat_cat_sit_id”.

7. Alembic, assuming this reflected table is just a plain table reflected from 
the DB, is making the slight misjudgment that .name and .key are the same, this 
could be improved, but generally it’s looking at column.name and then in that 
error, requesting that name from table.c.

if it were me, I’d probably limit the use of that event to Table objects that 
you know are being created from programmatic code.   Table accepts a parameter 
“listeners” so that you can create events that are local to that Table as it is 
being created:  
http://docs.sqlalchemy.org/en/rel_0_9/core/metadata.html#sqlalchemy.schema.Table.params.listeners.
  Or you can, in your event, look at table.metadata and make sure its the 
“metadata” object that you know is associated with your setup (eg. 
table.metadata is Base.metadata, something like that).  Or put a flag in 
table.info, or metadata.info (OK metadata.info is only in 1.0, but something 
like that), and check that.  




> 
> Thanks for any suggestions to fix this.
> Dewey
> 
> INFO  [alembic.autogenerate.compare] Detected added foreign key 
> (ctr_gem_id)(ctr_gem_id) on table cat_ctr_att
> 
> INFO  [alembic.autogenerate.compare] Detected added foreign key 
> (dtw_id)(dtw_id) on table cat_ctr_att
> 
> INFO  [alembic.autogenerate.compare] Detected added foreign key 
> (sit_id)(sit_id) on table cat_ctr_att
> 
> Traceback (most recent call last):
> 
>   File "/Users/dgaedcke/Virtualenvs/paysys/bin/alembic", line 9, in <module>
> 
>     load_entry_point('alembic==0.7.2', 'console_scripts', 'alembic')()
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/config.py",
>  line 399, in main
> 
>     CommandLine(prog=prog).main(argv=argv)
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/config.py",
>  line 393, in main
> 
>     self.run_cmd(cfg, options)
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/config.py",
>  line 376, in run_cmd
> 
>     **dict((k, getattr(options, k)) for k in kwarg)
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/command.py",
>  line 113, in revision
> 
>     script.run_env()
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/script.py",
>  line 382, in run_env
> 
>     util.load_python_file(self.dir, 'env.py')
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/util.py",
>  line 241, in load_python_file
> 
>     module = load_module_py(module_id, path)
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/compat.py",
>  line 79, in load_module_py
> 
>     mod = imp.load_source(module_id, path, fp)
> 
>   File "alembic/env.py", line 74, in <module>
> 
>     run_migrations_online()
> 
>   File "alembic/env.py", line 67, in run_migrations_online
> 
>     context.run_migrations()
> 
>   File "<string>", line 7, in run_migrations
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/environment.py",
>  line 742, in run_migrations
> 
>     self.get_context().run_migrations(**kw)
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/migration.py",
>  line 296, in run_migrations
> 
>     for step in self._migrations_fn(heads, self):
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/command.py",
>  line 95, in retrieve_migrations
> 
>     autogen._produce_migration_diffs(context, template_args, imports)
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/autogenerate/api.py",
>  line 145, in _produce_migration_diffs
> 
>     autogen_context, object_filters, include_schemas)
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/autogenerate/api.py",
>  line 247, in _produce_net_changes
> 
>     inspector, metadata, diffs, autogen_context)
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/autogenerate/compare.py",
>  line 118, in _compare_tables
> 
>     inspector)
> 
>   File 
> "/usr/local/Cellar/python/2.7.8_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/contextlib.py",
>  line 24, in __exit__
> 
>     self.gen.next()
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/alembic/autogenerate/compare.py",
>  line 208, in _compare_columns
> 
>     if _run_filters(conn_table.c[cname], cname,
> 
>   File 
> "/Users/dgaedcke/Virtualenvs/paysys/lib/python2.7/site-packages/sqlalchemy/util/_collections.py",
>  line 157, in __getitem__
> 
>     return self._data[key]
> 
> KeyError: u'cat_cat_sit_id'
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy-alembic" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy-alembic+unsubscr...@googlegroups.com 
> <mailto:sqlalchemy-alembic+unsubscr...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout 
> <https://groups.google.com/d/optout>.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy-alembic+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to