SQLalchemy AttributeError: 'str' 对象没有属性 '_sa_instance_state' [英] SQLalchemy AttributeError: 'str' object has no attribute '_sa_instance_state'

查看:23
本文介绍了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('company.compID'))
    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)
session.commit()

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

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"应该与之相关.这个对象是通过浏览器表单正确添加的,我通过我的网络服务器脚本启用了该表单.不过,我相信我应该能够直接从终端添加项目.

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' 对象没有属性 '_sa_instance_state'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆