SQLalchemy AttributeError:'str'对象没有属性'

编程入门 行业动态 更新时间:2024-10-24 08:21:03
本文介绍了SQLalchemy AttributeError:'str'对象没有属性'_sa_instance_state'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用SQLAlchemy + Python将项目添加到我的数据库中,但是一直出现错误.

I'm trying to add an item to my database with SQLAlchemy + Python, but keep getting an error.

我的database_setup.py:

My database_setup.py:

class company(Base): __tablename__ = 'company' compID = Column(Integer, primary_key = True) name = Column(String(80), nullable = False) class item(Base): __tablename__ = 'items' itemID = Column(Integer, primary_key = True) name = Column(String(80), nullable = False) category = Column(String(250)) description = Column(String(250)) price = Column(String(8)) compID = Column(Integer, ForeignKey('companypID')) company = relationship(company)

在将sqlalchemy导入终端后,我定义了一个要插入的项目:

after importing sqlalchemy to my terminal, I define an item to insert:

JawboneUP3 = item( itemID="1", name="Jawbone UP3", description="The latest UP!", category="tracker", price="$174.99", company="Jawbone" )

并绘制一个会话以添加和提交:

and draw a session to add and commit:

session.add(JawboneUP3) sessionmit()

提交时,我不断收到此错误:

When I submit, I keep getting this error:

Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1399, in add self._save_or_update_state(state) File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1417, in _save_or_update_state halt_on=self._contains_state): File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 2037, in cascade_iterator parent_dict, visited_states, halt_on)) File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/properties.py", line 932, in cascade_iterator get_all_pending(state, dict_) File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/attributes.py", line 761, in get_all_pending ret = [(instance_state(current), current)] AttributeError: 'str' object has no attribute '_sa_instance_state'

我在我的公司表中添加了一个"Jawbone"对象,我理解我的"JawboneUP3"应该与之相关.该对象是通过我通过Web服务器脚本启用的浏览器表单正确添加的.我相信我应该可以直接从终端添加项目.

I have added a 'Jawbone' object to my company table, that I understand my 'JawboneUP3' should relate back to. This object was added correctly through a browser form that I enabled via my webserver script. I believe I should be able to add items right from the terminal though.

推荐答案

我认为问题在于您如何定义相关的公司架构:

I think the problem is in how you are defining the related company schema:

JawboneUP3 = item(itemID = "1", name = "Jawbone UP3", description = "The latest UP!", category = "tracker", price = "$174.99", company = "Jawbone") # HERE^

item构造函数需要一个company实例,但是您正在传递字符串值.修复它:

The item constructor expects a company instance but you are passing a string value. Fix it:

JawboneUP3 = item(itemID="1", name="Jawbone UP3", description="The latest UP!", category="tracker", price="$174.99", company=company(name="Jawbone"))

更多推荐

SQLalchemy AttributeError:'str'对象没有属性'

本文发布于:2023-10-16 16:45:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1498154.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   对象   AttributeError   SQLalchemy   str

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!