Discussion:
Error "object has no attribute '_sa_instance_state'" - one to many (with fixtures)
Vinz
2012-05-18 16:08:03 UTC
Permalink
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 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
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Michael Bayer
2012-05-18 21:03:11 UTC
Permalink
Post by Vinz
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).
Post by Vinz
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".
Post by Vinz
Also, I don't quite understand the following warning from the fixture-
"
... 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
this isn't a SQLAlchemy question. Contact the author of that package for this.
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Vinz
2012-05-24 08:39:31 UTC
Permalink
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 !
Post by Michael Bayer
Post by Vinz
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...
 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
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).
Post by Vinz
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".
Post by Vinz
Also, I don't quite understand the following warning from the fixture-
"
...         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
this isn't a SQLAlchemy question.  Contact the author of that package for this.
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to ***@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+***@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Loading...