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

table name is not auto-generated while the primary key is defined in __table_args__ #533

Closed
WindSoilder opened this issue Aug 29, 2017 · 1 comment
Milestone

Comments

@WindSoilder
Copy link

Take these code as example:

app = Flask(__name__)
db = SQLAlchemy()
db.init_app(app)


class Plan(db.Model):
    __table_args__ = (
        db.PrimaryKeyConstraint('id', name='pk_plan'),
        db.UniqueConstraint('name', name='uq_plan')
     )
    id = db.Column(db.Integer)
    name = db.Column(db.String(50))

with app.app_context():
    db.create_all()

When try to generate the table, an error is occurred. The trace back information is something like this:

Traceback (most recent call last):
  File "model.py", line 13, in <module>
    class Plan(db.Model):
    File "/home/zero/study/python_env/for_cl/lib/python3.5/sitepackages/flask_sqlalchemy/__init__.py", line 603, in __init__
      DeclarativeMeta.__init__(self, name, bases, d)
    File "/home/zero/study/python_env/for_cl/lib/python3.5/sitepackages/sqlalchemy/ext/declarative/api.py", line 64, in __init__
      _as_declarative(cls, classname, cls.__dict__)
    File "/home/zero/study/python_env/for_cl/lib/python3.5/sitepackages/sqlalchemy/ext/declarative/base.py", line 88, in _as_declarative
       _MapperConfig.setup_mapping(cls, classname, dict_)
    File "/home/zero/study/python_env/for_cl/lib/python3.5/sitepackages/sqlalchemy/ext/declarative/base.py", line 103, in setup_mapping
      cfg_cls(cls_, classname, dict_)
    File "/home/zero/study/python_env/for_cl/lib/python3.5/sitepackages/sqlalchemy/ext/declarative/base.py", line 133, in __init__
      self._setup_inheritance()
    File "/home/zero/study/python_env/for_cl/lib/python3.5/sitepackages/sqlalchemy/ext/declarative/base.py", line 430, in _setup_inheritance
      "table-mapped class." % cls
sqlalchemy.exc.InvalidRequestError: Class <class '__main__.Plan'> does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class.

May be it can be enhanced by support the table defined schema like this ?

@davidism
Copy link
Member

This is handled by #541.

@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

2 participants