Fetch

The default behaviour for a query is to get only values from the target type's database table. If the type has relations to other types, they are not loaded. Delia does not do lazy loading.

The fetch function causes Delia to do an SQL JOIN and get the target type and the given relation. This is sometimes called eager loading.

Fetch

The fetch function loads the given relation using an SQL JOIN. Consider the following two types. Customer has an optional one-to-one relation to Address. Address has a mandatory one-to-one relation to Customer. Customer is the parent side of the relation and Address is the child. The foreign key is stored on the child side (Address).

 type Customer struct {
  id int primaryKey
  name string
  relation addr Address one optional
 } end
 type Address struct {
  id int primaryKey
  relation cust Customer one 
  street string
  city string
  state string
 } end

 let c = Customer[id > 10].fetch('addr')
 let city = c.addr.city //the customer's address has been loaded

FK

The fk function causes Delia to do an limited SQL JOIN and get the foreign key for the given relation.

In Delia, relations are defined in both sides of the relation. But the foreign key is only one one side. The default behaviour does not get the foreign key of the parent (since its not a field in the parent database table)

The fk function will get the foreign key. It can be thought of as a partial fetch.

 type Customer struct {
  id int primaryKey
  name string
  relation addr Address one optional
 } end
 type Address struct {
  id int primaryKey
  relation cust Customer one 
  street string
  city string
  state string
 } end

 let c = Customer[id > 10].fk('addr')
 let addressId = c.addr.id //customer's address FK has been loaded

The fk function has no effect if used on a child relation (since it already has the FK in its database table, which will be retrieved anyway).

Ony many-to-many relations, the fk function gets the FK from the association table.

FKs

The fks function causes Delia to get the foreign key(s) for all relations in the type.

 type Customer struct {
  id int primaryKey
  name string
  relation addr Address one optional
  relation profile UserProfile one optional
 } end
 type Address struct {
  id int primaryKey
  relation cust Customer one 
  street string
  city string
  state string
 } end
 type UserProfile struct {
  id int primaryKey
  relation cust Customer one 
  //other fields...
 } end

 let c = Customer[id > 10].fks()
 let addressId = c.addr.id //customer's address FK has been loaded
 let profileId = c.profile.id //customer's profile FK has been loaded