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

sqlalchemy error when foreign key relation has column and table with identical names #104

Open
JohnHBrock opened this issue May 2, 2015 · 0 comments

Comments

@JohnHBrock
Copy link

When using a postgres DB with the following tables and foreign key relationship...

CREATE TABLE bar
(
  id integer NOT NULL,
  value text,
  CONSTRAINT "PK_bar" PRIMARY KEY (id)
);

CREATE TABLE foo
(
  id integer NOT NULL,
  bar integer,
  CONSTRAINT "PK_foo" PRIMARY KEY (id),
  CONSTRAINT "FK_foo_bar" FOREIGN KEY (bar)
      REFERENCES bar (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
);

...I get the following error when running sandmanctl:

Traceback (most recent call last):
  File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "C:\Python27\lib\runpy.py", line 72, in _run_code
    exec code in run_globals
  File "C:\Python27\Scripts\sandmanctl.exe\__main__.py", line 9, in <module>
  File "C:\Python27\lib\site-packages\click\core.py", line 664, in __call__
    return self.main(*args, **kwargs)
  File "C:\Python27\lib\site-packages\click\core.py", line 644, in main
    rv = self.invoke(ctx)
  File "C:\Python27\lib\site-packages\click\core.py", line 837, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "C:\Python27\lib\site-packages\click\core.py", line 464, in invoke
    return callback(*args, **kwargs)
  File "C:\Python27\lib\site-packages\sandman\sandmanctl.py", line 43, in run
    activate(name='sandmanctl')
  File "C:\Python27\lib\site-packages\sandman\model\utils.py", line 188, in activate
    register_classes_for_admin(db.session, show_pks, name)
  File "C:\Python27\lib\site-packages\sandman\model\utils.py", line 160, in register_classes_for_admin
    admin_view.add_view(admin_view_class(cls, db_session))
  File "C:\Python27\lib\site-packages\flask_admin\contrib\sqla\view.py", line 289, in __init__
    menu_icon_value=menu_icon_value)
  File "C:\Python27\lib\site-packages\flask_admin\model\base.py", line 584, in __init__
    self._refresh_cache()
  File "C:\Python27\lib\site-packages\flask_admin\model\base.py", line 653, in _refresh_cache
    self._list_columns = self.get_list_columns()
  File "C:\Python27\lib\site-packages\flask_admin\model\base.py", line 729, in get_list_columns
    columns = self.scaffold_list_columns()
  File "C:\Python27\lib\site-packages\flask_admin\contrib\sqla\view.py", line 384, in scaffold_list_columns
    for p in self._get_model_iterator():
  File "C:\Python27\lib\site-packages\flask_admin\contrib\sqla\view.py", line 311, in _get_model_iterator
    return model._sa_class_manager.mapper.iterate_properties
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 1803, in iterate_properties
    configure_mappers()
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 2589, in configure_mappers
    mapper._post_configure_properties()
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\mapper.py", line 1694, in _post_configure_properties
    prop.init()
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\interfaces.py", line 144, in init
    self.do_init()
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\relationships.py", line 1549, in do_init
    self._process_dependent_arguments()
  File "C:\Python27\lib\site-packages\sqlalchemy\orm\relationships.py", line 1573, in _process_dependent_arguments
    setattr(self, attr, attr_value())
  File "C:\Python27\lib\site-packages\sqlalchemy\ext\declarative\clsregistry.py", line 271, in __call__
    x = eval(self.arg, globals(), self._dict)
  File "<string>", line 1, in <module>
  File "C:\Python27\lib\site-packages\sqlalchemy\ext\declarative\clsregistry.py", line 216, in __getattr__
    " directly to a Column)." % key)
sqlalchemy.exc.InvalidRequestError: Property 'bar' is not an instance of ColumnProperty (i.e. does not correspond directly to a Column).

The appears to be because of the foreign key between foo.bar and bar.id: the error happens whenever the foreign key column and parent table have the same name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant