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

NoForeignKeysError with polymorphism and schemas #512

Closed
ThiefMaster opened this issue Jun 27, 2017 · 3 comments
Closed

NoForeignKeysError with polymorphism and schemas #512

ThiefMaster opened this issue Jun 27, 2017 · 3 comments
Milestone

Comments

@ThiefMaster
Copy link
Contributor

ThiefMaster commented Jun 27, 2017

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///test'
db = SQLAlchemy(app)


class Item(db.Model):
    __tablename__ = 'items'
    __table_args__ = {'schema': 'foo'}  # this causes the error
    __mapper_args__ = {'polymorphic_on': 'type', 'polymorphic_identity': None}

    id = db.Column(db.Integer, primary_key=True)
    type = db.Column(db.Integer, nullable=False)
    parent_id = db.Column(db.ForeignKey('foo.items.id'), index=True, nullable=True)
    children = db.relationship('Item', backref=db.backref('parent', remote_side=[id]))


class SubItem(Item):
    __mapper_args__ = {
        'polymorphic_identity': 1
    }

traceback:

Traceback (most recent call last):
  File "flasksatest.py", line 21, in <module>
    class SubItem(Item):
  File "/home/adrian/dev/indico/env/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 602, in __init__
    DeclarativeMeta.__init__(self, name, bases, d)
  File "/home/adrian/dev/indico/env/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", line 64, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "/home/adrian/dev/indico/env/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 88, in _as_declarative
    _MapperConfig.setup_mapping(cls, classname, dict_)
  File "/home/adrian/dev/indico/env/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 103, in setup_mapping
    cfg_cls(cls_, classname, dict_)
  File "/home/adrian/dev/indico/env/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 135, in __init__
    self._early_mapping()
  File "/home/adrian/dev/indico/env/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 138, in _early_mapping
    self.map()
  File "/home/adrian/dev/indico/env/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 534, in map
    **self.mapper_args
  File "<string>", line 2, in mapper
  File "/home/adrian/dev/indico/env/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 671, in __init__
    self._configure_inheritance()
  File "/home/adrian/dev/indico/env/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 978, in _configure_inheritance
    self.local_table)
  File "<string>", line 2, in join_condition
  File "/home/adrian/dev/indico/env/lib/python2.7/site-packages/sqlalchemy/sql/selectable.py", line 979, in _join_condition
    (a.description, b.description, hint))
sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key relationships between 'items' and 'items'.

It works fine if I do not use a custom schema.

@davidism
Copy link
Member

I'm not really familiar with Postgres schemas, and I'm not sure a test for this would work with the SQLite backend. Would you verify that #541 fixes this?

@davidism
Copy link
Member

Have confirmation from Thief that this will be fixed by the tablename patch.

@halfdan
Copy link

halfdan commented Jan 25, 2019

I'm actually still seeing this error in the latest version.

Same setup only difference is: __mapper_args__ = {"polymorphic_identity": "geo_item", "concrete": True}

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

No branches or pull requests

3 participants