我有一个sqlalchemy反映的表,该表指向teradata数据库中的表。问题在于,反射表显然没有在主键上启动。
td_engine = create_engine('teradata://'+user+':'+pswd+'@'+host)
Session = sessionmaker()
Session.configure(bind=td_engine)
pswd = getpass('Password:')
sess = Session()
metadata = MetaData(td_engine)
Base = declarative_base(cls=DeferredReflection)
class myTable(Base):
__tablename__ = "table_name"
__table_args__ = {'schema': 'schema_name'}
Base.prepare(td_engine)
当我运行Base.prepare(td_engine)
时,会引发错误:
---------------------------------------------------------------------------
ArgumentError Traceback (most recent call last)
<ipython-input-9-f5961da6abab> in <module>()
1 # declarative_base class Base imported from table_defs along with table class defs
----> 2 Base.prepare(td_engine)
~/anaconda3/envs/teradata37/lib/python3.6/site-packages/sqlalchemy/ext/declarative/api.py in prepare(cls, engine)
691 for thingy in to_map:
692 cls._sa_decl_prepare(thingy.local_table, engine)
--> 693 thingy.map()
694 mapper = thingy.cls.__mapper__
695 metadata = mapper.class_.metadata
~/anaconda3/envs/teradata37/lib/python3.6/site-packages/sqlalchemy/ext/declarative/base.py in map(self)
641 def map(self):
642 self._configs.pop(self._cls, None)
--> 643 return super(_DeferredMapperConfig, self).map()
644
645
~/anaconda3/envs/teradata37/lib/python3.6/site-packages/sqlalchemy/ext/declarative/base.py in map(self)
574 self.cls,
575 self.local_table,
--> 576 **self.mapper_args
577 )
578 del self.cls._sa_declared_attr_reg
<string> in mapper(class_, local_table, properties, primary_key, non_primary, inherits, inherit_condition, inherit_foreign_keys, extension, order_by, 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)
~/anaconda3/envs/teradata37/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, extension, order_by, 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)
692 self._configure_properties()
693 self._configure_polymorphic_setter()
--> 694 self._configure_pks()
695 Mapper._new_mappers = True
696 self._log("constructed")
~/anaconda3/envs/teradata37/lib/python3.6/site-packages/sqlalchemy/orm/mapper.py in _configure_pks(self)
1325 "Mapper %s could not assemble any primary "
1326 "key columns for mapped table '%s'" %
-> 1327 (self, self.mapped_table.description))
1328 elif self.local_table not in self._pks_by_table and \
1329 isinstance(self.local_table, schema.Table):
ArgumentError: Mapper Mapper|myTable|table_name could not assemble any primary key columns for mapped table 'table_name'
我忽略实现的反射表类有一些参数吗?任何帮助,我们将不胜感激。