Comments (0)

Files changed (1)

doc/build/content/datamapping.txt

LIMIT 1 OFFSET 2

{}

+Theres also a way to combine scalar results with objects, using `add_column()`. This is often used for functions and aggregates. When `add_column()` (or its cousin `add_entity()`, described later) is used, tuples are returned:

+

+ {python}

+ result = session.query(User).add_column(func.max(users_table.c.name)).group_by([c for c in users_table.c]).all()

+ for r in result:

+ print "user:", r[0]

+ print "max name:", r[1]

+

Later in this chapter, we'll discuss how to configure relations between mapped classes. Once that's done, we'll discuss how to use table joins in [datamapping_joins](rel:datamapping_joins).

#### Loading by Primary Key {@name=primarykey}

{python}

l = session.query(User).join('addresses').

- filter(addresses_table.c.street=='123 Green Street').

+ filter_by(street='123 Green Street').

reset_joinpoint().filter_by(user_name='ed').all()

With `reset_joinpoint()`, we can also issue new `join()`s which will start back from the root table.

-In all cases, we can get the `User` and the matching `Address` objects back at the same time, by telling the session we want both. This returns the results as a tuple:

+In all cases, we can get the `User` and the matching `Address` objects back at the same time, by telling the session we want both. This returns the results as a list of tuples:

{python}

- l = session.query(User, Address).join('addresses').

+ result = session.query(User, Address).join('addresses').

filter(addresses_table.c.street=='123 Green Street').all()

- for result in l:

- print "User:", l[0]

- print "Address:", l[1]

+ for r in result:

+ print "User:", r[0]

+ print "Address:", r[1]

The above syntax is shorthand for using the `add_entity()` method:

{python}

session.query(User).add_entity(Address).join('addresses').all()

-Theres also a way to combine scalar results with objects, using `add_column()`. This is often used for functions and aggregates.

To join across multiple relationships, specify them in a list. Below, we load a `ShoppingCart`, limiting its `cartitems` collection to the single item which has a `price` object whose `amount` column is 47.95:

{python}

addresses_6ca7.user_id AS addresses_6ca7_user_id,

addresses_6ca7.zip AS addresses_6ca7_zip, addresses_6ca7.state AS addresses_6ca7_state,

+ WHERE addresses.street = :addresses_street ORDER BY users.oid, addresses_6ca7.oid

{'addresses_street': '123 Green Street'}

-The join implied by passing the "street" parameter is separate from the join produced by the eager join, which is "aliasized" to prevent conflicts.

+The join resulting from `join('addresses')` is separate from the join produced by the eager join, which is "aliasized" to prevent conflicts.

#### Using Options to Change the Loading Strategy {@name=options}

### More Relationships {@name=morerelations}

-Previously, we've discussed how to set up a one-to-many relationship. This section will go over the remaining major types of relationships that can be configured. More detail on on relationships as well as more advanced patterns can be found in [adv_datamapping](rel:adv_datamapping).

+Previously, we've discussed how to set up a one-to-many relationship. This section will go over the remaining major types of relationships that can be configured. More detail on on relationships as well as more advanced patterns can be found in [advdatamapping](rel:advdatamapping).