Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLalchemy AttributeError: 'str' object has no attribute '_sa_instance_state'

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

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)

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'

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.

like image 346
mleafer Avatar asked Oct 12 '15 14:10

mleafer


2 Answers

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^

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"))
like image 151
alecxe Avatar answered Nov 01 '22 20:11

alecxe


First of all, when defining a class in Python, it is of good practice to to start the names with an uppercase letter like this:

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)

That being said, it is not why your code throws an error. :)

The reason

The Item constructor expects an instance of the object Company to by passed as a value of the variable company

Solution 1: The Jawbone company does not exist in your database

Here the answer of @alecxe is valid.

You should replace your code with:

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

Adding this object to the session and comiting the changes will actually make two entries to your database:

  • The item entry, with name "Jawbone UP3"
  • A row in the company database, a company with name "Jawbone"

Solution 2: The Jawbone company exists in your database

Here you should fetch the company Jawbone from your table "company" and pass it as an argument to the Item constructor, like this:

jawbone = session.query(Company).filter_by(name="Jawbone").first()

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

For the session part check this

like image 14
Tiwtiw Avatar answered Nov 01 '22 19:11

Tiwtiw