- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试在基类中使用单个 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()
注意事项:
@declared_attr.cascading
,而只需要 @declared_attr
FilteringMixin
中,我重命名了使用基于字符串的语法来定义约束。关于在 __table_args__ 中使用 CheckConstraint 的 Flask-SQLAlchemy 混合导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34974021/
我发现如果 __table_args__ 存在于单表继承设置中的子类中,SQLAlchemy 会抛出 sqlalchemy.exc.ArgumentError: Can't place __table
我没有使用 sqlalchemy 的经验,我有以下代码: class ForecastBedSetting(Base): __tablename__ = 'forecast_bed_set
假设我有这样的表 class Base(object): id = Column(Integer, primary_key=True) date_updated = Column(Da
是否可以使用外键引用并在 sqlalchemy 中的同一个表上有 oracle 分区? 这是在 __table_args__ 中将 oracle 分区定义为字典的方式 class SQLAlchemy
我正在尝试在基类中使用单个 CheckConstraint 创建基模型。当我将基类混合到 flask-sqlalchemy 模型中并尝试使用它时,我得到“无法将未命名的列添加到列集合”。 我试着用不同
我正在尝试使用flask、sqlalchemy 和flask_migrate ... 但是每次运行 manage.py migrate 时,alembic 总是将我的模型检测为新表。 我想我把 tab
在 SQLAlchemy 中,我已经阅读了如何在声明声明时组合来自不同 mixin 的 __table_args__。 Combining Table/Mapper Arguments from Mu
我有以下 SQLAlchemy 模型。已成功迁移至数据库: class MyClassA(db.Model, Timestamp): a_id = db.Column(db.Integer,
我是一名优秀的程序员,十分优秀!