Arti
Arti

Reputation: 7762

Sqlalchemy returns tuple

I have two tables:

class Wishlist(db.Model):
    __tablename__ = 'wishlists'

    id = db.Column(db.Integer, primary_key=True)
    device_id = db.Column(db.Integer, nullable=False)

class Device(db.Model):
    __tablename__ = 'devices'

    id = db.Column(db.Integer, primary_key=True)
    unique_id = db.Column(db.String(255), nullable=False)

Every wishlist has Integer field with Device.id. When i select all wishlists and try to join Device object to query:

query = db.session.query(
        Wishlist,
        Device
    ).filter(
        ...
    ).join(
        Device,
        Device.id == Wishlist.device_id
    )

SqlAlchemy returns for me tuple:

(<app.main.models.Wishlist object at 0xa5cec2c>, <app.main.models.Device object at 0xa5d14ac>)

But how to set device object like wishlist attribute, to access for it wishlist.device.id

Upvotes: 2

Views: 4949

Answers (2)

dirn
dirn

Reputation: 20729

You specify both Wishlist and Device in your call to query. If you just want the former with the latter exposed through a property, only specify one.

query = db.session.query(
    Wishlist
).filter(
    ...
).join(
    Device,
    Device.id == Wishlist.device_id
)

Upvotes: 4

Ashalynd
Ashalynd

Reputation: 12563

Considering the examples in the documentation, something like this should work?

for (wishlist, device) in query:
    print wishlist.device_id, device.unique_id # etc...

Upvotes: 3

Related Questions