'InstanceState' object has no attribute 'manager.mapper', w/ Jython2.7.2

133 views
Skip to first unread message

Alexander Dorsk

unread,
Jun 6, 2012, 10:37:10 AM6/6/12
to sqlal...@googlegroups.com
Hello All,

I was curious if anyone else has run into this error:

<error message>

Traceback (most recent call last):
  File "util/sa/tests/test_sa_dao.py", line 96, in setUp
    self.session.add(tc1)
  File "/home/adorsk/projects/gr/jenv2.7/Lib/site-packages/sqlalchemy/orm/session.py", line 1251, in add
    self._save_or_update_state(state)
  File "/home/adorsk/projects/gr/jenv2.7/Lib/site-packages/sqlalchemy/orm/session.py", line 1262, in _save_or_update_state
    mapper = _state_mapper(state)
AttributeError: 'InstanceState' object has no attribute 'manager.mapper'

</error message>

The context in which this error message is generated is pasted below.

I'm using Jython2.7 on a Postgres, via xzJDBC and the Postgresql JDBC driver.

If you did encounter this error, did you find a workaround?

-Alex

P.S. SqlAlchemy is absolutely brilliant!  Beautiful docs and code, definitely my new favorite python tool.



<code>

import unittest

from sqlalchemy import Table, Column, ForeignKey, ForeignKeyConstraint, Integer, String, Float, MetaData, create_engine
from sqlalchemy.orm import relationship, mapper
from geoalchemy import *
from geoalchemy.postgis import PGComparator
from sqlalchemy.orm import sessionmaker

class My_Test(unittest.TestCase):

    def testFoo(self):
        print "foo"

    def setUp(self):
        self.engine = create_engine('postgresql+zxjdbc://MY_DB:MY_USER@localhost/MY_PASS)
        self.Session = sessionmaker()
        connection = self.engine.connect()

        # begin a non-ORM transaction
        self.trans = connection.begin()

        # bind an individual Session to the connection
        self.session = self.Session(bind=connection)

        schema = {}
        self.schema = schema
        schema['classes'] = {}

        class TestClass1(object):
            id = None
            children = []
        schema['classes']['TestClass1'] = TestClass1

        class TestClass2(object):
            id = None
            name = ""
        schema['classes']['TestClass2'] = TestClass2

        schema['primary_class'] = TestClass1

        metadata = MetaData()

        test1_table = Table('test1', metadata,
                Column('id', Integer, primary_key=True)
                )

        test2_table = Table('test2', metadata,
                Column('id', Integer, primary_key=True),
                Column('name', String)
                )

        test1_test2_table = Table('test1_test2', metadata,
                Column('test1_id', Integer, primary_key=True),
                Column('test2_id', Integer, primary_key=True),
                ForeignKeyConstraint(['test1_id'], [test1_table.c.id]),
                ForeignKeyConstraint(['test2_id'], [test2_table.c.id])
                )

        mapper(
                TestClass1,
                test1_table,
                properties = {
                    'children': relationship(TestClass2, secondary=test1_test2_table)
                    }
                )

        mapper(
                TestClass2,
                test2_table,
                properties = {
                    }
                )

        metadata.create_all(self.session.bind)

        tc1s = []
        tc2s = []
        for i in range(5):
            tc1 = TestClass1()
            tc1s.append(tc1)
            self.session.add(tc1)

            tc2 = TestClass2()
            tc2.name = "tc2_%s" % i
            tc2s.append(tc2)
            self.session.add(tc2)

        self.session.commit()

        for i in range(len(tc1s)):
            tc1 = tc1s[i]
            child_tc2s = [tc2s[i], tc2s[ (i + 1) % len(tc1s)]]
            for c in child_tc2s:
                tc2 = self.session.query(TestClass2).filter(TestClass2.id == c.id).one()
                tc1.children.append(tc2)
      
        self.session.commit()

if __name__ == '__main__':
    unittest.main()

</code>

Michael Bayer

unread,
Jun 6, 2012, 11:02:38 AM6/6/12
to sqlal...@googlegroups.com
unfortunately issues like these are often resulting from Jython bugs.    For example, SQLAlchemy was entirely unusable with the previous version of Jython due to a bug in their __import__ mechanism.  That the test works fine using regular cPYthon with psycopg2 further points to some incompatibility/quirk in Jython as a potential culprit.   

As a test, since I don't have Jython installed, what does this produce for you ?

class Foo(object):
    pass

class Bar(object):
    pass

f = Foo()
f.bar = Bar()
f.bar.bat = 5

from operator import attrgetter
print attrgetter("bar.bat")(f)

if you get that same error, then this is the Jython bug - attrgetter() as of 2.6 handles dotted paths.   SQLAlchemy does have a "workaround" version for Python less than 2.6 which we can also enable for Jython, for the interim, but also this should be reported to Jython as a bug.

SA User

unread,
Jun 6, 2012, 11:33:59 AM6/6/12
to sqlalchemy
Ah, you're right, it does look like a Jython bug.

When I run the code you provided above I get the error below, which
does show that it's a Jython issue.

Traceback (most recent call last):
File "t.py", line 13, in <module>
print attrgetter("bar.bat")(f)
AttributeError: 'Foo' object has no attribute 'bar.bat'


I'll let the Jython folks know.

Thanks for the fast response, you saved me a lot of head-banging and
code spelunking.

-Alex

Reply all
Reply to author
Forward
0 new messages