[sqlalchemy] Re: scalar association_proxy

2011-01-20 Thread AgentOrange

 Maybe you can squelch the exception by using a synonym with the descriptor 
 argument to map to a fake property.

Oooh, I had not come across this construct before - looks as if it
might do exactly what I want. I might even be able to make a decorator
that wraps this all up nicely. Thank you very much; I shall let you
know how I get on!

-- 
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.



[sqlalchemy] scalar association_proxy

2011-01-19 Thread AgentOrange
Hi Folks,

I have been scratching my head over this one all day, so any advice
would be greatly appreciated!

I have a pretty simple join table setup like this:

foo foo_bar  bar
---
id  id_foo (unique)  id
id_bar
more_stuff

It looks like a many-to-many relationship, but it's not, since id_foo
is unique in the join table.

- There can be any number of 'foo' for each 'bar'
- Each 'foo' has exactly one or zero 'bar'

I have set up the required declarative stuff, and an association proxy
in 'foo' to get to 'bar' in one nice step. Since there can only ever
be one (or zero - and this is my problem) I have set it to be a
scalar.

It all works great, except that if I try and look at the value of the
assoication proxy in a 'foo' row without a corresponding 'foo_bar',
then I get AttributeError. It is clearly looking for a 'bar' in a
'foo_bar' that is a None (since there is no entry), so is
understandable; but in my case not desirable.

What I would like to do is to get a 'bar' if one exists, else return a
'None'. Is the only way to do this to write my own wrapper property
that does a try/catch? And if I do this, will the property stop being
nice and magical (you know, filterable and comparable etc..)

I am quite a newcomer to SQLAlchemy so go easy on me!

All the best,

Philip

-- 
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.



[sqlalchemy] Re: scalar association_proxy

2011-01-19 Thread AgentOrange
Hi ho,

Thanks for the swift reply!

 Did you use uselist=False on foo.bar and the associationproxy? It would nice 
 if you could show some 
 code.http://www.sqlalchemy.org/docs/orm/extensions/associationproxy.html?h...

Yes I did. The code is more complicated as it has a bunch of other
stuff in it, but here's a condensation:

class Foo(Base):
id = Column(Integer, primary_key = True)
foo_bar_join = relationship(FooBar, primaryjoin = (id ==
FooBar.id_foo), uselist = False)
bar = association_proxy('foo_bar_join', 'bar', creator = lambda x:
FooBar(bar = x) )

class Bar(Base):
id = Column(Integer, primary_key = True)

class FooBar(Base):
id_foo = Column(Integer, ForeignKey('foo.id'), primary_key = True)
id_bar = Column(Integer, ForeignKey('bar.id'), primary_key = True)

bar = relationship(Bar, primaryjoin = (id_bar == Bar.id))
other_stuff = Column(Integer)

The problem comes if you try to fetch 'bar' from an instance of 'Foo'
for which there is no entry in the foo_bar join table.

-- 
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.