pyeve / eve-sqlalchemy

SQLAlchemy data layer for Eve-powered RESTful APIs
http://eve-sqlalchemy.readthedocs.io
Other
234 stars 70 forks source link

How to properly get related objects? #88

Closed johnnywell closed 7 years ago

johnnywell commented 8 years ago

I couldn't find any example how to load objects like in a ManyToMany relationship, whether a list of ids or embedded objects. Am I missing something?

johnnywell commented 8 years ago

Well I just figured out, the simplest way is through the relationship backref argument, this just returns the list of ids. For a rich embedded response you need to configure it on DOMAIN['resource']['schema'] like is shown on Eve documentation http://python-eve.org/features.html#embedded-resource-serialization.

johnnywell commented 8 years ago

Actually it isn't working with ManyToMany using a secondary table, anybody has an idea how to put it to work?

joystein commented 8 years ago

I'm also wondering about this.

Maybe association proxy could help: http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#simplifying-association-objects

There is a test with association proxy here: https://github.com/RedTurtle/eve-sqlalchemy/blob/master/eve_sqlalchemy/tests/test_association_proxy.py

Maybe that can serve as a starting point.

johnnywell commented 8 years ago

Thanks, I'll take a look at those links.

On Mon, Nov 23, 2015 at 12:11 PM, Øystein S. Haaland < notifications@github.com> wrote:

I'm also wondering about this.

Maybe association proxy could help:

http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#simplifying-association-objects

There is a test with association proxy here:

https://github.com/RedTurtle/eve-sqlalchemy/blob/master/eve_sqlalchemy/tests/test_association_proxy.py

Maybe that can serve as a starting point.

— Reply to this email directly or view it on GitHub https://github.com/RedTurtle/eve-sqlalchemy/issues/88#issuecomment-158941665 .

Johnny W. dos Santos

johnnywell commented 8 years ago

It didn't quite worked, the max I got was the absence of the attribute in objects where it actually has related objects.

mandarvaze commented 8 years ago

@johnnywell can you provide some sample code/model definition here ? Your partial success could help others (like me), as well as someone may be able to help you get "full success" Which versions of eve-sqlalchemy and eve are you using ?

I'm on Eve 0.5.3 and Eve-SQLAlchemy 0.3.4 I worked around this problem using on_fetched hook (and updating the output using DB query) I know it is hack-y/inelegant, but works :(

BTW, No #84 seems related.

johnnywell commented 8 years ago

I tried the models from the proxy association example on SQLAlchemy documentation, I've just added the fields required for eve, like _created, _update and _etag. http://docs.sqlalchemy.org/en/rel_1_0/_modules/examples/association/proxied_association.html

I'm using the same versions as you.

May you show your "work around"? It may be enough for now.

On Wed, Nov 25, 2015 at 5:46 AM, Mandar Vaze notifications@github.com wrote:

@johnnywell https://github.com/johnnywell can you provide some sample code/model definition here ? Your partial success could help others (like me), as well as someone may be able to help you get "full success" Which versions of eve-sqlalchemy and eve are you using ?

I'm on Eve 0.5.3 and Eve-SQLAlchemy 0.3.4 I worked around this problem using on_fetched hook (and updating the output using DB query) I know it is hack-y/inelegant, but works :(

BTW, No #84 https://github.com/RedTurtle/eve-sqlalchemy/issues/84 seems related.

— Reply to this email directly or view it on GitHub https://github.com/RedTurtle/eve-sqlalchemy/issues/88#issuecomment-159526746 .

Johnny W. dos Santos

joystein commented 8 years ago

I would also very much like to know if anyone succeeds in making this work.

jeroenes commented 8 years ago

I stumbled across this older post - Please recognise that Johnny has closed this one, see his link. If you follow the sqlalchemy docs on relationships it works, see http://docs.sqlalchemy.org/en/latest/orm/relationships.html.

I have the following working, including embedding in Eve: association_a = Table('association_a',Base.metadata, Column(b_id',Integer, ForeignKey('b.id')), Column('a_id',Integer, ForeignKey('a.id')), ) and in the table b have: a = relationship('A', secondary=association_a, backref="b") and in a b_id = Column(Integer, ForeignKey('b.id'))

It took me a while to figure this one out though, hope this helps!

Cheers, Jeroen