(First time poster here, sorry if I break any rules. I tried to find
info on formatting code etc, but failed.)

Versions used: Python 2.7, Jython 2.5.2 (w/ Java 1.6), SQLAlchemy
0.7.3, PostgreSQL 9.1

I'm having some trouble getting started with SQLAlchemy. I've been
following along with this tutorial: 
http://www.sqlalchemy.org/docs/orm/tutorial.html.
The following code executes fine under Python (as in, CPython).

    from sqlalchemy import create_engine, Column, Integer, String
    from sqlalchemy.ext.declarative import declarative_base
    Base = declarative_base()

    engine = create_engine('sqlite:///:memory:', echo = True)
    engine.execute("select 1").scalar()

    class User(Base):
        __tablename__ = 'users'

        id       = Column(Integer, primary_key = True)
        name     = Column(String)
        fullname = Column(String)
        password = Column(String)

        def __init__(self, name, fullname, password):
            self.name = name
            self.fullname = fullname
            self.password = password

        def __repr__(self):
            return "<User('%s', '%s', '%s')>" % (self.name,
self.fullname, self.password)

    print User.__table__

Output:

    2011-11-17 16:53:24,076 INFO sqlalchemy.engine.base.Engine select
1
    2011-11-17 16:53:24,076 INFO sqlalchemy.engine.base.Engine ()
    users

However if I try to run the above code on Jython, with the only
modification being to change the string passed to create_engine() to
"postgresql+zxjdbc://jacksonc:abc123@localhost:5432/testdb", it
doesn't work. I get the following output when the User class is
declared:

    2011-11-17 16:55:50,443 INFO sqlalchemy.engine.base.Engine select
current_schema()
    2011-11-17 16:55:50,459 INFO sqlalchemy.engine.base.Engine ()
    2011-11-17 16:55:50,536 INFO sqlalchemy.engine.base.Engine select
1
    2011-11-17 16:55:50,536 INFO sqlalchemy.engine.base.Engine ()
    Traceback (most recent call last):
      File "H:\Eclipse Projects\sandboxJython\src\sandbox
\storage2.py", line 8, in <module>
        class User(Base):
      File "C:\jython2.5.2\Lib\site-packages\sqlalchemy\ext
\declarative.py", line 1259, in __init__
        _as_declarative(cls, classname, cls.__dict__)
      File "C:\jython2.5.2\Lib\site-packages\sqlalchemy\ext
\declarative.py", line 1002, in _as_declarative
        class_mapped = _is_mapped_class(base)
      File "C:\jython2.5.2\Lib\site-packages\sqlalchemy\orm\util.py",
line 658, in _is_mapped_class
        if isinstance(cls, (AliasedClass, mapperlib.Mapper)):
      File "C:\jython2.5.2\Lib\site-packages\sqlalchemy\util
\langhelpers.py", line 611, in __getattr__
        raise AttributeError(
    AttributeError: Module sqlalchemy.orm.mapperlib has no attribute
'Mapper'

I was able to get some SQLAlchemy+Jython code to run, following the
tutorial here:http://www.jython.org/jythonbook/en/1.0/
DatabasesAndJython.html. This is the code:

    from sqlalchemy import create_engine, Table, Column, Integer,
String, MetaData
    from sqlalchemy.orm import mapper

    db = create_engine('postgresql+zxjdbc://jacksonc:abc123@localhost:
5432/testdb', echo = True)
    db.execute("select 1").scalar()

    metadata = MetaData()
    user = Table('user', metadata,
                 Column('id',       Integer, primary_key=True),
                 Column('name',     String()),
                 Column('fullname', String()),
                 Column('password', String()))
    metadata.create_all(db)

    class User(object):
        def __init__(self, name, fullname, password):
            self.name = name
            self.fullname = fullname
            self.password = password

        def __repr__(self):
            return "<User('%s', '%s', '%s')>" % (self.name,
self.fullname, self.password)

    print mapper(User, user)

and this is the output:

    2011-11-17 16:45:23,609 INFO sqlalchemy.engine.base.Engine select
current_schema()
    2011-11-17 16:45:23,609 INFO sqlalchemy.engine.base.Engine ()
    2011-11-17 16:45:23,703 INFO sqlalchemy.engine.base.Engine select
1
    2011-11-17 16:45:23,703 INFO sqlalchemy.engine.base.Engine ()
    2011-11-17 16:45:23,720 INFO sqlalchemy.engine.base.Engine select
relname from pg_class c join pg_namespace n on n.oid=c.relnamespace
where n.nspname=current_schema() and relname=?
    2011-11-17 16:45:23,750 INFO sqlalchemy.engine.base.Engine
(u'user',)
    2011-11-17 16:45:23,812 INFO sqlalchemy.engine.base.Engine
    CREATE TABLE "user" (
        id SERIAL NOT NULL,
        name VARCHAR,
        fullname VARCHAR,
        password VARCHAR,
        PRIMARY KEY (id)
    )


    2011-11-17 16:45:23,812 INFO sqlalchemy.engine.base.Engine ()
    2011-11-17 16:45:24,640 INFO sqlalchemy.engine.base.Engine COMMIT
    Mapper|User|user

I've done a lot of googling, but the only similar thing I found was
this google cached pastebin. It exhibits the same problem, but that
doesn't really help me:
http://webcache.googleusercontent.com/search?q=cache:mWs96xzoH1sJ:pastebin.com/nd0s7d75+%22AttributeError:+Module+sqlalchemy.orm.mapperlib+has+no+attribute+Mapper%22&cd=1&hl=en&ct=clnk&gl=au

Anyone have any ideas why the declarative_base method doesn't work on
Jython, given that it works for me on CPython, and I *can* get other
SQLAlchemy code to run on Jython?

Sorry for the long post, I didn't want to leave anything out in case
it was relevant.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to