SQLAlchemy never seems to work correctly in IDLE

Issue #778 resolved
Former user created an issue

Cannot map tables to classes. The following is the error that results from following the example in the SQLAlchemy 0.4.0 documentation found at the following URL: http://www.sqlalchemy.org/docs/04/ormtutorial.html

----- Output:

Python 2.5.1 (r251:54863, Apr 18 2007, 08:51:08) v.1310 32 bit (Intel) on win32 Type "copyright", "credits" or "license()" for more information.

****************************************************************
Personal firewall software may warn about the connection IDLE
makes to its subprocess using this computer's internal loopback
interface.  This connection is not visible on any external
interface and no data is sent to or received from the Internet.
****************************************************************

IDLE 1.2.1 ==== No Subprocess ====

import sqlalchemy sqlalchemy.version '0.4.0beta5' from sqlalchemy import create_engine engine = create_engine('sqlite:///:memory:', echo=True) from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey metadata = MetaData() users_table = Table('users', metadata,Column('id', Integer, primary_key=True),Column('name', String(40)),Column('fullname', String(100)),Column('password', String(15))) metadata.create_all(engine) class User(object): def init(self, name, fullname, password): self.name = name self.fullname = fullname self.password = password

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

from sqlalchemy.orm import mapper mapper(User, users_table) <sqlalchemy.orm.mapper.Mapper object at 0x037A7A70> ed_user = User('ed', 'Ed Jones', 'edspassword')

Traceback (most recent call last): File "<pyshell#15>", line 1, in <module> ed_user = User('ed', 'Ed Jones', 'edspassword') File "C:\program files\Python25\lib\site-packages\sqlalchemy\orm\mapper.py", line 677, in init self.compile() File "C:\program files\Python25\lib\site-packages\sqlalchemy\orm\mapper.py", line 220, in compile self.compile_all() File "C:\program files\Python25\lib\site-packages\sqlalchemy\orm\mapper.py", line 235, in _compile_all mapper._do_compile() File "C:\program files\Python25\lib\site-packages\sqlalchemy\orm\mapper.py", line 257, in _do_compile self.__log("_do_compile() started") File "C:\program files\Python25\lib\site-packages\sqlalchemy\orm\mapper.py", line 157, in __log self.logger.info("(" + self.class.name + "|" + (self.entity_name is not None and "/%s" % self.entity_name or "") + (self.local_table and self.local_table.name or str(self.local_table)) + (not self._is_primary_mapper() and "|non-primary" or "") + ") " + msg) AttributeError: 'str' object has no attribute 'name'

Comments (6)

  1. jek

    Is that your entire session? This works for me (and passes the doctest).

    $ python -i
    Python 2.5.1 (440caee41b881047eb6f145c0477737bdece8eaf:54863, May 23 2007, 16:25:53) 
    [4.1.1 20070105 (Red Hat 4.1.1-52)](GCC) on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import sqlalchemy
    >>> sqlalchemy.__version__
    'svn'
    >>> from sqlalchemy import create_engine
    >>> engine = create_engine('sqlite:///:memory:', echo=True)
    >>> from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey   
    >>> metadata = MetaData()
    >>> users_table = Table('users', metadata,
    ...                     Column('id', Integer, primary_key=True),
    ...                     Column('name', String(40)),
    ...                     Column('fullname', String(100)),
    ...                     Column('password', String(15))
    ... )
    >>> 
    >>> metadata.create_all(engine) 
    2007-09-12 11:40:44,665 INFO sqlalchemy.engine.base.Engine.0x..cL PRAGMA table_info("users")
    2007-09-12 11:40:44,665 INFO sqlalchemy.engine.base.Engine.0x..cL {}
    2007-09-12 11:40:44,666 INFO sqlalchemy.engine.base.Engine.0x..cL 
    CREATE TABLE users (
            id INTEGER NOT NULL, 
            name VARCHAR(40), 
            fullname VARCHAR(100), 
            password VARCHAR(15), 
            PRIMARY KEY (id)
    )
    
    
    2007-09-12 11:40:44,667 INFO sqlalchemy.engine.base.Engine.0x..cL ()
    2007-09-12 11:40:44,667 INFO sqlalchemy.engine.base.Engine.0x..cL COMMIT
    >>> class User(object):
    ...     def __init__(self, name, fullname, password):
    ...         self.name = name
    ...         self.fullname = fullname
    ...         self.password = password
    ...     def __repr__(self):
    ...         return "<User(%r,%r, %r)>" % (self.name, self.fullname, self.password)
    ... 
    >>> from sqlalchemy.orm import mapper
    >>> mapper(User, users_table) 
    <sqlalchemy.orm.mapper.Mapper object at 0xb7a8c04c>
    >>> ed_user = User('ed', 'Ed Jones', 'edspassword')
    >>>
    
  2. Former user Account Deleted

    Strange - it works if I run a script containing the code from the documentation example from a Windows command line. However, if I try to run the same script from an IDLE python shell, I get the AttributeError?: 'str' object has no attribute 'name' error. Any ideas why this is happening?

  3. Mike Bayer repo owner

    sqlalchemy behaves very weirdly in IDLE, for currently unknown reasons. Seems like idle has too much magic and we have too much....alchemy ;)

    someone will have to sit down and figure out just whats going on in that regard. Its not unheard of that SA has some slightly weird getattr or import behavior that throws off some of these tools (like pydoc wasnt working with the sql.py/expressions.py module until recently, for example, due to a single getattr method).

  4. Log in to comment