我正在尝试将从sql-alchemy查询返回的字典传递给扭曲的Perspective Broker,但是我收到以下错误:
Unpersistable('Unpersistable data: instance of class sqlalchemy.sql.elements._truncated_label deemed insecure')
在调查时我发现查询返回以下内容:
我的标签 <class 'sqlalchemy.sql.elements._truncated_label'>
有人可以解释我如何避免这种情况吗?
以下是Table
声明之一:
module_tbl = Table(
"module",
MetaData(),
Column("id", Integer(), primary_key=True),
Column("name", String()),
Column("description", String()),
Column("context", Integer()),
Column("scope", Integer()),
Column("send_body", Boolean()),
Column("direction", Integer()),
Column("level", Integer()),
Column("incoming_order", Integer()),
Column("outgoing_order", Integer()),
Column("created_at", DateTime()),
Column("updated_at", DateTime()), )
这是代码错误:
@inlineCallbacks
def get_config(mname, domains, direction):
engine = yield get_default_engine()
# try to retrieve the module database model, raise on fail
mod_tbl = getattr(ddl, mname + '_tbl')
if mod_tbl is None:
raise ModuleBindException('Could not find a database model for module <%s>' % mname)
# convert domains to list
if not isinstance(domains, list):
domains = [domains]
_select = [ddl.domain_module_tbl, ddl.domain_tbl, ddl.module_tbl, mod_tbl]
_from = ddl.domain_module_tbl
_join = [(ddl.domain_tbl, ddl.domain_module_tbl.c.domain_id ==
func.COALESCE(ddl.domain_tbl.c.master_domain_id, ddl.domain_tbl.c.id)),
(mod_tbl, ddl.domain_module_tbl.c.id == mod_tbl.c.domain_module_id),
(ddl.module_tbl, ddl.domain_module_tbl.c.module_id == ddl.module_tbl.c.id), ]
_where = [ddl.domain_module_tbl.c.enabled == 1,
ddl.domain_module_tbl.c.direction.op('&')(direction),
ddl.module_tbl.c.name == mname, # optional but enforces security on blind join
ddl.domain_tbl.c.name.in_(tuple(domains))]
for j in _join:
_from = _from.join(*j)
_stmt = select(_select).select_from(_from)
for w in _where:
_stmt = _stmt.where(w)
# use aliasing
_stmt = _stmt.apply_labels()
# query
result = yield engine.execute(_stmt)
config = yield result.fetchall()
config_per_domain = dict()
mod_prefix = '%s_' % mname
for c in config:
# rename the mod_{name}_* keys to mod_* so it's easier to access from modules
cfg = dict((k, v) for k, v in dict(c).items() if not k.startswith(mod_prefix))
mod_cfg = dict((str('mod_%s' % k[len('%s_' % mname):]), v) for k, v in dict(c).items()
if k.startswith(mod_prefix))
cfg.update(mod_cfg)
config_per_domain[cfg.get('domain_name')] = cfg
defer.returnValue(config_per_domain)
答案 0 :(得分:0)
_truncated_label
是unicode
(或Python 3中的str
)的子类。如果您使用的库已正确编写,则会使用isinstance
而非type()
来检查某些内容是否为字符串。
无论如何,这个子类通常用于列,索引和类似数据库对象的名称,当您需要在自己的代码中使用这些名称时,您只需使用unicode(whatever)
而不仅仅whatever
如果您使用的是Python 3,则将其转换为普通的unicode对象(str
)。