Flask-SQLAlchemy mixin与__table_args__中的CheckConstraint导致错误

时间:2016-01-24 09:16:45

标签: flask sqlalchemy flask-sqlalchemy

我正在尝试使用基类中的单个CheckConstraint创建基本模型。当我将基类混入flask-sqlalchemy模型并尝试使用它时,我得到“无法将未命名的列添加到列集合中”。

我尝试使用不同的方式来声明此CheckConstraint 但都是由于同样的错误造成的。是什么原因引起了这个?

from sqlalchemy.ext.declarative import declared_attr
from myproject.db import db


class FilteringMixin(db.Model):
    __abstract__ = True

    FILTER_TYPES = {
        'whitelist': 0,
        'blacklist': 1,
    }

    @declared_attr.cascading
    def filter_type(cls):
        return db.Column(db.SmallInteger, nullable=False,
                         default=cls.FILTER_TYPES['whitelist'])

    @declared_attr.cascading
    def __table_args__(cls):
        return (
                 db.CheckConstraint(cls.filter_type.in_(cls.FILTER_TYPES.values())),
        )


class FilteredConnectionType(FilteringMixin, db.Model):
    CONNECTION_TYPES = {
        'wifi': 0,
        'cellular': 1,
    }

    __tablename__ = 'filtered_connection_types'

    id = db.Column(db.BigInteger, primary_key=True)

    connection_type = db.Column(db.SmallInteger, nullable=False,
                                default=CONNECTION_TYPES['cellular'])

    @declared_attr.cascading
    def __table_args__(cls):
        return (
            db.CheckConstraint(cls.connection_type.in_(cls.CONNECTION_TYPES.values())),
        ) + FilteringMixin.__table_args__

在那之后,尝试使用它我得到:

python manage.py shell  
~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py:173: SAWarning: Unmanaged access of declarative attribute __table_args__ from non-mapped class FilteringMixin
  (desc.fget.__name__, cls.__name__))
~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py:173: SAWarning: Unmanaged access of declarative attribute filter_type from non-mapped class FilteringMixin
  (desc.fget.__name__, cls.__name__))
Traceback (most recent call last):
  File "manage.py", line 4, in <module>
    from myproject import create_app, environments, scripts
  File "~/myuser/myproject/myproject/__init__.py", line 9, in <module>
    import models
  File "~/myuser/myproject/myproject/models/__init__.py", line 13, in <module>
    from .filtering_implemented import FilteredConnectionType
  File "~/myuser/myproject/myproject/models/filtering_implemented.py", line 6, in <module>
    class FilteredConnectionType(FilteringMixin, db.Model):
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/flask_sqlalchemy/__init__.py", line 609, in __init__
    DeclarativeMeta.__init__(self, name, bases, d)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/api.py", line 55, in __init__
    _as_declarative(cls, classname, cls.__dict__)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 88, in _as_declarative
    _MapperConfig.setup_mapping(cls, classname, dict_)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 103, in setup_mapping
    cfg_cls(cls_, classname, dict_)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 131, in __init__
    self._setup_table()
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/ext/declarative/base.py", line 394, in _setup_table
    **table_kw)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 416, in __new__
    metadata._remove_table(name, schema)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 411, in __new__
    table._init(name, metadata, *args, **kw)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 488, in _init
    self._init_items(*args)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 72, in _init_items
    item._set_parent_with_dispatch(self)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/sql/base.py", line 433, in _set_parent_with_dispatch
    self._set_parent(parent)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 2553, in _set_parent
    ColumnCollectionMixin._set_parent(self, table)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/sql/schema.py", line 2521, in _set_parent
    self.columns.add(col)
  File "~/myuser/myproject/.venv/local/lib/python2.7/site-packages/sqlalchemy/sql/base.py", line 509, in add
    "Can't add unnamed column to column collection")
sqlalchemy.exc.ArgumentError: Can't add unnamed column to column collection

1 个答案:

答案 0 :(得分:2)

我认为真正的问题在错误输出顶部的两个警告中突出显示:

.../api.py:173: SAWarning: Unmanaged access of declarative attribute __table_args__ from non-mapped class FilteringMixin
  (desc.fget.__name__, cls.__name__))

而且,据我所知,它是由FilteringMixin.__table_args__使用引起的,修复方法是使用基于字符串的语法来描述约束:

class FilteringMixin(object):
    FILTER_TYPES = {
        'whitelist': 0,
        'blacklist': 1,
    }

    @declared_attr
    def filter_type(cls):
        return db.Column(db.SmallInteger, nullable=False,
                         default=cls.FILTER_TYPES['whitelist'])

    @declared_attr
    def __table_args__(cls):
        return (
             db.CheckConstraint(
                 'filter_type in (%s)' %
                 ','.join(str(t) for t in cls.FILTER_TYPES.values())),
        )


class FilteredConnectionType(FilteringMixin, db.Model):
    CONNECTION_TYPES = {
        'wifi': 0,
        'cellular': 1,
    }

    __tablename__ = 'filtered_connection_types'
    # Changed to db.Integer from db.BigInteger because of SQLite
    id = db.Column(db.Integer, primary_key=True)
    connection_type = db.Column(db.SmallInteger, nullable=False,
                                default=CONNECTION_TYPES['cellular'])

    @declared_attr
    def __table_args__(cls):
        return (
            db.CheckConstraint(cls.connection_type.in_(cls.CONNECTION_TYPES.values())),
        ) + FilteringMixin.__table_args__

测试代码:

# saves the record with defaults
conn_type = FilteredConnectionType()
session.add(conn_type)
session.commit()

# raises an error: Check constraint failed
conn_type = FilteredConnectionType(filter_type=3)
session.add(conn_type)
session.commit()

注意:

  • FilteringMixin扩展了对象,而不是db.Model
  • 我不是百分百确定,但您可能不需要@declared_attr.cascading,只需@declared_attr
  • FilteringMixin我重命名了使用的基于字符串的语法来定义约束。
  • 完整示例为here
  • sqlalchemy文档参考 - declared_arrtMixin and Custom Base Classes