#525 better support for sqlalchemy

open
nobody
None
5
2011-03-21
2011-03-21
Werner F. Bruhin
No

When working with sqlalchemy it would be nice if code completion could help with finding column name of a defined table and that code analyses does not mark these columns as "undefined variable from import"

Sample:
# -*- coding: utf-8 -*-#

import sasession

# sasession.db.Region_Ls.name - is marked as "Undefined variable from import"
reg = sasession.session.query(sasession.db.Region_Ls).order_by(sasession.db.Region_Ls.name).first()

something = reg

# no code completion on the following
print something.name
print something.country_ls.name

I attach a zip file containing:
- createDemoDb.py, run it to create the sqlite test db
- sasession.py, sets up the sa engine, session etc
- saTestRun.py, contains the above test

Discussion

  • Here is another example. The following code shows how SQLAlchemy is typically used in a web fraemwork like SQLAlchemy, I've modified it so that it can be run as a standalone script (replace leading dots with spaces). The problem here is that PyDev marks the "add" members and the "query" member in the last lines as errors:

    ------------

    from sqlalchemy import create_engine, Column, Integer, String
    from sqlalchemy.orm import scoped_session, sessionmaker
    from sqlalchemy.ext.declarative import declarative_base

    Base = declarative_base()

    class User(Base):
    ....__tablename__ = 'users'

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

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

    ....def __repr__(self):
    ........return "%s (%s)" % (self.name, self.password)

    # problem 1: scoped sessions
    session = scoped_session(sessionmaker())
    # problem 2: query property
    Base.query = session.query_property()

    engine = create_engine('sqlite:///:memory:')
    session.configure(bind=engine)
    Base.metadata.create_all(bind=engine)

    ed = User('Ed', 'secret')
    joe = User('Joe', 'topsecret')

    session.add(ed) # add unknown
    session.add(joe) # add member unknown

    print User.query.order_by(User.name).all() # query member unknown

     
  • Moritz Schlarb
    Moritz Schlarb
    2012-03-16

    Same issue here with a Pylons project.
    I think the problem lies within the concept of paster setting up the configuration prior to executing all the modules.
    So if there was an option to specify an alternative launch command (e.g. paster shell development.ini) - which could be nicely packed as a new project type, that issue could proably be solved.