通过排除表来处理自动Map错误

hs1ihplo  于 2021-09-08  发布在  Java
关注(0)|答案(0)|浏览(171)

我使用sqlalchemy的automap从由odoo管理的postgres数据库自动生成模式(直接访问数据库比使用odooapi快)。但以下文档中的例子给了我一些问题。
我试图根据此处的建议仅解析特定表:


# we can reflect it ourselves from a database, using options

# such as 'only' to limit what tables we look at...

metadata.reflect(engine, only=['user', 'address'])

我的代码:

import sqlalchemy as sa
from sqlalchemy.ext.automap import automap_base
from app import odb
from sqlalchemy.orm import declarative_base, relationship
from sqlalchemy.orm.relationships import RelationshipProperty

metadata = sa.MetaData()
metadata.reflect(odb, only=['sale_order'])
Base = automap_base(metadata=metadata)
Base.prepare()
Order = Base.classes.sale_order

但是我在一个我没有指定的表的Map上出错了。

[ins] In [1]: from app.omodels import Order
/home/mas/pe/app/omodels/omodels.py:8: SAWarning: Skipped unsupported reflection of expression-based index res_partner_vat_index
  metadata.reflect(odb, only=['sale_order'])
---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
<ipython-input-1-39a04770d5b4> in <module>
----> 1 from app.omodels import Order

~/pe/app/omodels/__init__.py in <module>
----> 1 from .omodels import *

~/pe/app/omodels/omodels.py in <module>
      8 metadata.reflect(odb, only=['sale_order'])
      9 Base = automap_base(metadata=metadata)
---> 10 Base.prepare()
     11 Order = Base.classes.sale_order
     12

<string> in prepare(cls, autoload_with, engine, reflect, schema, classname_for_table, collection_class, name_for_scalar_relationship, name_for_collection_rela
tionship, generate_relationship, reflection_options)

~/pe/.venv/lib/python3.6/site-packages/sqlalchemy/util/deprecations.py in warned(fn, *args,**kwargs)
    296                         stacklevel=3,
    297                     )
--> 298             return fn(*args,**kwargs)
    299
    300         doc = fn.__doc__ is not None and fn.__doc__ or ""

~/pe/.venv/lib/python3.6/site-packages/sqlalchemy/ext/automap.py in prepare(cls, autoload_with, engine, reflect, schema, classname_for_table, collection_class
, name_for_scalar_relationship, name_for_collection_relationship, generate_relationship, reflection_options)
    926
    927             for map_config in _DeferredMapperConfig.classes_for_base(cls):
--> 928                 map_config.map()
    929
    930     _sa_decl_prepare = True

~/pe/.venv/lib/python3.6/site-packages/sqlalchemy/orm/decl_base.py in map(self, mapper_kw)
   1070     def map(self, mapper_kw=util.EMPTY_DICT):
   1071         self._configs.pop(self._cls, None)
-> 1072         return super(_DeferredMapperConfig, self).map(mapper_kw)
   1073
   1074

~/pe/.venv/lib/python3.6/site-packages/sqlalchemy/orm/decl_base.py in map(self, mapper_kw)
    990         return self.set_cls_attribute(
    991             "__mapper__",
--> 992             mapper_cls(self.cls, self.local_table,**self.mapper_args),
    993         )
    994

<string> in __init__(self, class_, local_table, properties, primary_key, non_primary, inherits, inherit_condition, inherit_foreign_keys, always_refresh, versi
on_id_col, version_id_generator, polymorphic_on, _polymorphic_map, polymorphic_identity, concrete, with_polymorphic, polymorphic_load, allow_partial_pks, batc
h, column_prefix, include_properties, exclude_properties, passive_updates, passive_deletes, confirm_deleted_rows, eager_defaults, legacy_is_orphan, _compiled_
cache_size)

~/pe/.venv/lib/python3.6/site-packages/sqlalchemy/util/deprecations.py in warned(fn, *args,**kwargs)
    296                         stacklevel=3,
    297                     )
--> 298             return fn(*args,**kwargs)
    299
    300         doc = fn.__doc__ is not None and fn.__doc__ or ""
~/pe/.venv/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py in __init__(self, class_, local_table, properties, primary_key, non_primary, inherits, inherit_condition, inherit_foreign_keys, always_refresh, version_id_col, version_id_generator, polymorphic_on, _polymorphic_map, polymorphic_identity, concrete, with_polymorphic, polymorphic_load, allow_partial_pks, batch, column_prefix, include_properties, exclude_properties, passive_updates, passive_deletes, confirm_deleted_rows, eager_defaults, legacy_is_orphan, _compiled_cache_size)
    681             self._configure_inheritance()
    682             self._configure_class_instrumentation()
--> 683             self._configure_properties()
    684             self._configure_polymorphic_setter()
    685             self._configure_pks()

~/pe/.venv/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py in _configure_properties(self)
   1439
   1440             self._configure_property(
-> 1441                 column_key, column, init=False, setparent=True
   1442             )
   1443

~/pe/.venv/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py in _configure_property(self, key, prop, init, setparent)
   1674
   1675         if not isinstance(prop, MapperProperty):
-> 1676             prop = self._property_from_column(key, prop)
   1677
   1678         if isinstance(prop, properties.ColumnProperty):

~/pe/.venv/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py in _property_from_column(self, key, prop)
   1866                 "use the 'include_properties' or 'exclude_properties' "
   1867                 "mapper arguments to control specifically which table "
-> 1868                 "columns get mapped." % (key, self, column.key, prop)
   1869             )
   1870

ArgumentError: WARNING: when configuring property 'product_packaging' on mapped class stock_move->stock_move, column 'product_packaging' conflicts with property '<RelationshipProperty at 0x7fe61ba87f48; product_packaging>'. To resolve this, map the column to the class under a different name in the 'properties' dictionary.  Or, to remove all awareness of the column entirely (including its availability as a foreign key), use the 'include_properties' or 'exclude_properties' mapper arguments to control specifically which table columns get mapped.

问:我如何排除这个表(或者只包含我想要的表),这样sqla就不会不必要地解析我不需要的表(解析这个数据库似乎也要花很长时间,因为它太大了)?
或者,如何覆盖此特定错误,以便访问 Order 对象错误/警告说
要解决此问题,请将列Map到“properties”字典中其他名称下的类。或者,要完全删除该列的所有感知(包括其作为外键的可用性),请使用“include_properties”或“exclude_properties”Map器参数来具体控制Map哪些表列。
但是我还没有描述这个表的模式,我不知道在哪里放置“exclude\u properties”。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题