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

InvalidRequestError: Table … is already defined for this MetaData instance (table inheritance on v2.2) #478

Closed
homeworkprod opened this issue Mar 1, 2017 · 7 comments
Milestone

Comments

@homeworkprod
Copy link

On Flask-SQLAlchemy 2.2 (Flask 0.12, SQLAlchemy 1.1.6, Python 3.5.3) I the following traceback that does not occur with Flask-SQLAlchemy 2.1:

Traceback (most recent call last):
  File "application.py", line 19, in <module>
    class ChildA(Parent):
  File "/home/user/project/venv/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 602, in __init__
    DeclarativeMeta.__init__(self, name, bases, d)
  File "/home/user/project/venv/lib/python3.5/site-packages/sqlalchemy/ext/declarative/api.py", line 64, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "/home/user/project/venv/lib/python3.5/site-packages/sqlalchemy/ext/declarative/base.py", line 88, in _as_declarative
    _MapperConfig.setup_mapping(cls, classname, dict_)
  File "/home/user/project/venv/lib/python3.5/site-packages/sqlalchemy/ext/declarative/base.py", line 103, in setup_mapping
    cfg_cls(cls_, classname, dict_)
  File "/home/user/project/venv/lib/python3.5/site-packages/sqlalchemy/ext/declarative/base.py", line 131, in __init__
    self._setup_table()
  File "/home/user/project/venv/lib/python3.5/site-packages/sqlalchemy/ext/declarative/base.py", line 395, in _setup_table
    **table_kw)
  File "/home/user/project/venv/lib/python3.5/site-packages/sqlalchemy/sql/schema.py", line 418, in __new__
    "existing Table object." % key)
sqlalchemy.exc.InvalidRequestError: Table 'items' is already defined for this MetaData instance.  Specify 'extend_existing=True' to redefine options and columns on an existing Table object.

Somewhat minimal code to reproduce:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy


db = SQLAlchemy()


class Parent(db.Model):
    __tablename__ = 'items'
    __mapper_args__ = {
        'polymorphic_identity': 'parent',
        'polymorphic_on': 'type',
    }

    id = db.Column(db.Integer, primary_key=True)
    type = db.Column(db.Unicode(10), nullable=False)


class ChildA(Parent):
    __mapper_args__ = {
        'polymorphic_identity': 'child_a',
    }

    foo = db.Column(db.Integer)


class ChildB(Parent):
    __mapper_args__ = {
        'polymorphic_identity': 'child_b',
    }

    bar = db.Column(db.Integer)


app = Flask(__name__)
app.run()

Unless I've been doing that wrong the whole time and just got away with it by chance, I think this might be unintended behavior on Flask-SQLAlchemy's side.

@ThiefMaster
Copy link
Contributor

See #473 / #470

@homeworkprod
Copy link
Author

Yeah, read those earlier, but couldn't make a sufficiently clear connection; my example above derives from db.Model and doesn't use @declared_attr.

@ThiefMaster
Copy link
Contributor

I think it's a general problem with polymorphy and the latest changes in Flask-SA. I'd pin the version to 2.1 until this is fixed - I don't think there's any workaround for now

@homeworkprod
Copy link
Author

Thanks, will do that.

@davidism davidism added the bug label Mar 1, 2017
@davidism davidism added this to the 2.2.1 milestone Mar 1, 2017
MarekSuchanek added a commit to MarekSuchanek/repocribro that referenced this issue Mar 2, 2017
There is some bug in Flask-SQLAlchemy 2.2, until it will be fixed version will be pinned to 2.1. Also I will consider leaving this extension, work with just SQLAlchemy and write needed constructs on my own. More about the bug: pallets-eco/flask-sqlalchemy#478
@davidism
Copy link
Member

This test from #541 should cover this case. Tablenames and tables are not created for single-table inheritance subclasses.

Until 2.3 is released, if you didn't just pin 2.1, you could set __tablename__ = None for STI.

@homeworkprod
Copy link
Author

v2.3.0 seems to have fixed the issue for me, thanks!

9034725985 added a commit to kusl/flaskexperiment that referenced this issue Dec 17, 2017
@davidism
Copy link
Member

If you have a new issue, please open a new ticket with an example demonstrating it.

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