Pythons SQLAlchemy 无法识别主键列并抱怨,为什么?
我有这个 MySQL 表:
CREATE TABLE `affiliations` (
`affiliation_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(333) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`affiliation_id`),
UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
和这个模型:
class Affiliation(Base):
__tablename__ = 'affiliations'
__table_args__ = {'autoload : True'}
def __init__(self, **kwargs):
for k, v in kwargs.iteritems():
setattr(self, k, v)
def __repr__(self):
return 'Affiliation({name!r}'.format(name=self.name)
当我启动
session = database.get_session(engine)
aff = session.query(Affiliation).first()
print repr(aff)
SQLAlchemy 时返回此错误:
sqlalchemy.exc.ArgumentError: Mapper Mapper|Affiliation|affiliations could not assemble any primary key columns for mapped table 'affiliations'
我已经多次重写数据库模型,仔细检查每个名称,更改列,但我找不到 SQLAlchemy 抱怨的原因。 正如您在表的CREATE STATMENT
中看到的,定义了一个主键。为什么 SQLAlchemy 找不到它?
我已经等效地定义了其他表,并且代码运行得很好。
I have this MySQL Table:
CREATE TABLE `affiliations` (
`affiliation_id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(333) COLLATE utf8_bin NOT NULL,
PRIMARY KEY (`affiliation_id`),
UNIQUE KEY `name_UNIQUE` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_bin
and this Model:
class Affiliation(Base):
__tablename__ = 'affiliations'
__table_args__ = {'autoload : True'}
def __init__(self, **kwargs):
for k, v in kwargs.iteritems():
setattr(self, k, v)
def __repr__(self):
return 'Affiliation({name!r}'.format(name=self.name)
When I initiate
session = database.get_session(engine)
aff = session.query(Affiliation).first()
print repr(aff)
SQLAlchemy returns this Error:
sqlalchemy.exc.ArgumentError: Mapper Mapper|Affiliation|affiliations could not assemble any primary key columns for mapped table 'affiliations'
I have rewritten the model an the database several times, doublechecked every name, altered columns, but I can't find why SQLAlchemy complains. As you can see in the CREATE STATEMENT
of the table, there is a Primary Key defined. Why can't SQLAlchemy find it?
I have defined other tables equivalently and the code works great.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论