SQLAlchemy Automap 不会为没有主键的表创建类 [英] SQLAlchemy Automap does not create class for tables without primary key
问题描述
我正在使用具有自动映射功能的 SQL Alchemy v(0.9.1).这允许我自动创建类和关系.http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/automap.html
I am using SQL Alchemy v(0.9.1) which has the automap functionality. This allows me to create classes and relationships automatically. http://docs.sqlalchemy.org/en/rel_0_9/orm/extensions/automap.html
我遇到的问题是,在使用 automap_base 时,我发现并没有映射 metadata.tables 列表中的所有可用表.
The problem I am experiencing is that when using automap_base, I see that not all the tables that are available in the metadata.tables list are mapped.
准备时没有错误,除了在调用 automap_base() 后我无法从 Base 访问类(例如 Base.classes.Example_Table)
There are no errors when preparing, except that I am unable to access the class (e.g. Base.classes.Example_Table) from the Base after calling automap_base()
from sqlalchemy import create_engine, MetaData
from sqlalchemy.orm import create_session
from sqlalchemy.ext.automap import automap_base
engine = create_engine("mysql+mysqldb://")
Base = automap_base()
Base.prepare(engine, reflect = True)
session = create_session(bind = engine)
然后我运行发现元数据中的所有表都不存在类(我没有在元数据中只使用 = [] 参数)
Then I run to find that classes do not exist for all tables in metadata (I did not use only = [] argument in metadata)
for mappedclass in Base.classes:
print mappedclass
for mdtable in metadata.tables:
print mdtable
才发现Example_Table(用下面的create语法)没有类
Only to find that Example_Table (with the following create syntax) does not have a class
CREATE TABLE `Example_Table` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`attributeType` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3290719 DEFAULT CHARSET=latin1
即Base.class.Example_Table 返回以下错误
i.e. Base.class.Example_Table returns the following error
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-15-94492ae1b8ba> in <module>()
7 type(metadata.tables)
8
----> 9 Base.classes.Example_Table
/usr/local/lib/python2.7/site-packages/sqlalchemy/util/_collections.pyc in __getattr__(self, key)
172 return self._data[key]
173 except KeyError:
--> 174 raise AttributeError(key)
175
176 def __contains__(self, key):
AttributeError: Example_Table
我认为这个问题不会发生,因为我的 Example_Table 名称中有一个下划线.
I don't think this problem happens because my Example_Table name has an underscore in it.
我认为问题与我的 Example_Table 没有主键这一事实有关.Example_Table 仅用于链接另外两个表.
I think the problem is related to the fact that my Example_Table does not have a primary key. The Example_Table is only meant to link two other tables.
推荐答案
通过梳理参考资料/重构问题解决了问题.
Figured it out by combing through the reference/reframing the problem.
如果它帮助别人 -
因为 SQLAlchemy ORM 基于身份映射模型,所以不能映射(或自动映射)没有主键的表.应指定任意主键.
Because SQLAlchemy ORM is based on an identity map model, one cannot map (or automap) a table that does not have a primary key. An arbitrary primary key should be specified.
这篇关于SQLAlchemy Automap 不会为没有主键的表创建类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!