hi all, 

i have a webapp with an existing database-model ``site``, including users. 
in a second service i create a new database-model ``market``, but still 
want to access the users (which works via separate engine).

goal: i want to create a relation from the ``market``-model to the 
``site``-model. e.g. referencing a ``User`` instance from the 
``site``-model to the ``market``-model. like this::


SITE_DATABASE_SCHEMA_NAME = 'site'
MARKET_DATABASE_SCHEMA_NAME = 'market'

def initialize_sql(engine, user_engine = None):
    DBSession = scoped_session(sessionmaker(bind=engine))
    DBSession.configure(bind=engine)
    log.debug('bind market engine')
    Base.metadata.bind = engine
    # danger: here we try to autogenerate the schema
    # Base.metadata.create_all(engine)

    if user_engine:
        log.debug('bind user engine')
        DeclarativeBase.metadata.bind = user_engine

class ProductItem(Base):
    """
    cross-db references:
    - 
http://stackoverflow.com/questions/6433592/cross-database-join-in-sqlalchemy
    - http://markmail.org/message/z5tdtlcuoth2osqm

    schemas:
    http://www.postgresql.org/docs/9.1/static/ddl-schemas.html
    """
    __tablename__ = 'product_items'
    __table_args__ = {'schema': MARKET_DATABASE_SCHEMA_NAME}

    id = sa.Column(sa.BigInteger,
                   Sequence('place_seq_id', optional=True),
                   primary_key=True)
    name = Column(Unicode(255), unique=True)
    user_id = sa.Column(
        sa.BigInteger,
        sa.ForeignKey('%s.%s.id' % (SITE_DATABASE_SCHEMA_NAME, 
User.__tablename__)),
        primary_key=True
    )
    # this one fails
*    user = sa.orm.relationship("User")*


question: 

- is this possible?
- how can i attach the missing ``User`` class to the known classes for that 
sqlalchemy session? now of course i get::

sqlalchemy.exc.InvalidRequestError: When initializing mapper 
Mapper|ProductItem|product_items, expression 'User' failed to locate a name 
("name 'User' is not defined"). If this is a class name, consider adding 
this relationship() to the <class 
'if_market.models.ProductItem.ProductItem'> class after both dependent 
classes have been defined.

thanks a lot! 

best, andi

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to