gpt4 book ai didi

在 __table_args__ 中使用 CheckConstraint 的 Flask-SQLAlchemy 混合导致错误

转载 作者:行者123 更新时间:2023-12-03 16:33:57 24 4
gpt4 key购买 nike

我正在尝试在基类中使用单个 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

最佳答案

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

.../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
  • 我不是 100% 确定,但您可能不需要 @declared_attr.cascading,而只需要 @declared_attr
  • FilteringMixin 中,我重命名了使用基于字符串的语法来定义约束。
  • 完整的例子是here
  • sqlalchemy 文档引用 - declared_arrtMixin and Custom Base Classes

关于在 __table_args__ 中使用 CheckConstraint 的 Flask-SQLAlchemy 混合导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34974021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com