On Oct 31, 2010, at 9:05 AM, Alessandro Dentella wrote:
> Hi,
>
> I have a join between 2 tables (User/Adresses, complete code below). I create
> a mapper as
> the join of the 2 classes as:
>
> m = orm.mapper(Join, User.__table__.outerjoin(Address.__table__) ,
> properties = {
> 'j_id' : [Address.__table__.c.user_id, User.__table__.c.id ]
> }
> )
>
> Now I run the query sess.query(m).all() and get:
>
> [<Join sandro ([email protected])>, None]
>
> I don't really understand why one row is None, it's clearly the row that has
> a user w/o address, the row that I want to see (otherwise I would have just
> used join).
>
> I guess my problem is in the way I setup the mapper but I can't understand
> how I should configure it. I read
> http://www.sqlalchemy.org/docs/orm/mapper_config.html#mapping-a-class-against-multiple-tables
> but could not understand eather...
if this is 0.5, your mapper requires allow_null_pks=True for a mapping to an
outer join. In 0.6 it's called allow_partial_pks and defaults to True.
>
> any hints?
>
> thanks in advanced
>
> *:-)
>
>
> --------------------------------------------
> The complete code of my example:
>
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy import Table, Column, ForeignKey, text, func
> from sqlalchemy.types import *
> from sqlalchemy import orm
>
> Base = declarative_base()
> Base.metadata.bind = 'sqlite://'
> Session = orm.sessionmaker(bind=Base.metadata.bind)
> sess = Session()
>
> class User(Base):
> __tablename__ = 'user'
> id = Column(Integer, primary_key=True)
> name = Column(String(30), nullable=False)
>
> class Address(Base):
> __tablename__ = 'address'
> aid = Column(Integer, primary_key=True)
> address = Column(String(30), nullable=True)
> user_id = Column(ForeignKey(User.id), nullable=False)
>
> user = orm.relation(User, backref='addresses', lazy=False)
>
> Base.metadata.create_all()
>
> u = User(name='sandro')
> b = User(name='bianco')
>
> sess.add(u)
> sess.add(b)
>
> sess.commit()
>
> a = Address(address='[email protected]', )
> a.user = u
> sess.add(a)
> sess.commit()
>
> class Join(object):
> def __repr__(self):
> return "<Join %s (%s)>" % (self.name, getattr(self, 'address', None))
>
> m = orm.mapper(Join, User.__table__.outerjoin(Address.__table__) ,
> properties = {
> 'j_id' : [Address.__table__.c.user_id, User.__table__.c.id ]
> }
> )
>
> q = sess.query(m)
> print q.all()
>
>
>
>
> --
> Sandro Dentella *:-)
> http://www.reteisi.org Soluzioni libere per le scuole
> http://sqlkit.argolinux.org SQLkit home page - PyGTK/python/sqlalchemy
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.