Error "object has no attribute '_sa_instance_state'" - one to many (with fixtures)

9,004 views
Skip to first unread message

Vinz

unread,
May 18, 2012, 12:08:03 PM5/18/12
to sqlalchemy
Hi all,
I'm writing some tests for my SqlAlchemy DB using the fixtures module,
and I'm currently trying to populate a one to many relationship.
http://farmdev.com/projects/fixture/using-dataset.html#referencing-foreign-dataset-classes

We have a relationship between Author and Address : (very much like
the SA doc)
------------ model.py ---------------
class Address(Base):
author_id = Column(Integer, ForeignKey('author.id'))
author = relationship("Author")
------------------------------------

Then I defined some data (fixture specific) :
------------ modelData.py --------------
class AuthorDate(DataSet):
class foo:
name = "foo"

class AddressData(DataSet):
class my_address:
name = "my address"
author_id = AuthorData.foo.ref('id')
author = [AuthorData.foo,] # this is handy with the fixtures
-------------

Then we have a file with the tests :
-------- testAuthor.py
import model
from modelData import *
# call to __init__.py and Base.metadata.bind = engine, sessionmaker
# definition of dbfixture

class TestAuthor(DataTestCase, unittest.TestCase):
#datasets = [AuthorData, etc]
#call to Base.metadata.create_all()
self.data = dbfixture.data(AuthorData, etc)
self.setup() # <--------- supposed to load all datasets and
populate self.data <---- raises the error
---------------------

And I get the following error when I run my tests :
Traceback (most recent call last):
File "tests/testCard.py", line 88, in setUp
self.data.setup()
File "/usr/local/lib/python2.6/dist-packages/fixture/base.py", line
71, in setup
[ bla bla ... ]
File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
attributes.py", line 654, in get_all_pending
ret = [(instance_state(current), current)]
LoadError: AttributeError: 'list' object has no attribute
'_sa_instance_state' (with 'my_address' of
'<fixture.dataset.dataset.my_address object at 0x26a64d0>' in
<AddressData at 0x22b3850 with keys ['my_address']>)

Do you have any idea about what my problem is ??!

Where can I find some documentation about _sa_instance_state ? (didn't
find what that is)

Also, I don't quite understand the following warning from the fixture-
s doc :
"
>>> class Books(DataSet):
... class two_worlds:
... title = "Man of Two Worlds"
... authors = [Authors.frank_herbert, Authors.brian_herbert]

However, in some cases you may need to reference an attribute that
does not have a value until it is loaded, like a serial ID column.
(****Note that this is not supported by the SQLAlchemy data layer when
using sessions****.) To facilitate this, each inner class of a DataSet
gets decorated with a special method, ref(), that can be used to
reference a column value before it exists, i.e.:
"
It may be related (though I understand they are talking about
ref('id') and that isn't my concern (yet) (I used it in other tests
and had no error messages).

Hope I was clear.
Many thanks in advance !

Vincent, not much experienced in SA and python

Michael Bayer

unread,
May 18, 2012, 5:03:11 PM5/18/12
to sqlal...@googlegroups.com

On May 18, 2012, at 12:08 PM, Vinz wrote:

> Hi all,
> I'm writing some tests for my SqlAlchemy DB using the fixtures module,
> and I'm currently trying to populate a one to many relationship.
> http://farmdev.com/projects/fixture/using-dataset.html#referencing-foreign-dataset-classes
>
> File "/usr/local/lib/python2.6/dist-packages/fixture/base.py", line
> 71, in setup
> [ bla bla ... ]
> File "/usr/local/lib/python2.6/dist-packages/sqlalchemy/orm/
> attributes.py", line 654, in get_all_pending
> ret = [(instance_state(current), current)]
> LoadError: AttributeError: 'list' object has no attribute
> '_sa_instance_state' (with 'my_address' of

OK just for future reference, the "bla bla" part here is actually quite important in revealing what the issue is. In this case it is likely that you're appending a list somewhere, where a mapped object is expected, such as:

myobject.some_attribute = []

or

myobject.some_collection.append([])

but there might be configurational issues with the testing package you're using that's leading to this (so perhaps check with them).

>
> Where can I find some documentation about _sa_instance_state ? (didn't
> find what that is)

yeah unfortunate python issue here, this is SQLAlchemy attempting to procure an internal tracking object present on all mapped objects called "InstanceState". We use an "attribute getter" to get this, which is because it's very fast compared to a plain function, but the downside is in a lot of areas of the code we aren't transforming this attribute error into a nicely descriptive message (something which can be improved). The error means, "'list' instance is not a mapped object".

>
> Also, I don't quite understand the following warning from the fixture-
> s doc :
> "
>>>> class Books(DataSet):
> ... class two_worlds:
> ... title = "Man of Two Worlds"
> ... authors = [Authors.frank_herbert, Authors.brian_herbert]
>
> However, in some cases you may need to reference an attribute that
> does not have a value until it is loaded, like a serial ID column.
> (****Note that this is not supported by the SQLAlchemy data layer when
> using sessions****.) To facilitate this, each inner class of a DataSet
> gets decorated with a special method, ref(), that can be used to
> reference a column value before it exists, i.e.:

this isn't a SQLAlchemy question. Contact the author of that package for this.


Vinz

unread,
May 24, 2012, 4:39:31 AM5/24/12
to sqlalchemy
OK, the "_sa_instance_state" message was kind of cryptic, but it
actually contained accurate information (that the problem came from my
list, badly configured).
Thanks !

On May 18, 11:03 pm, Michael Bayer <mike...@zzzcomputing.com> wrote:
> On May 18, 2012, at 12:08 PM, Vinz wrote:
>
> > Hi all,
> > I'm writing some tests for my SqlAlchemy DB using the fixtures module,
> > and I'm currently trying to populate a one to many relationship.
> >http://farmdev.com/projects/fixture/using-dataset.html#referencing-fo...
Reply all
Reply to author
Forward
0 new messages