Foreign key field

#1

I would like to know what is happening behind the scenes for foreign-key fields: Is there a specific table / schema that gets created for each foreign key field?

We often only see the foreign identifier (such as [ForeignType] (key_to_my_type)) where the second argument is implicitly the id of the ForeignType but I have seen syntaxes such as [ForeignType] (key_to_my_type, foreign_key_different_than_id). Where can I see documentation / examples on how to use foreign keys?

0 Likes

#2

Foreign key fields do not change the schema. They are resolved at query-time.

Lets say in your example you had:

entity type Foo {
  other: [Bar] (foreign_key)
}

First of all, this implies that you ALSO have

entity type Bar {
  foreign_key: Foo
}

When you do:

Foo.fetch({filter: Filter.eq("id", "a_foo")})

The platform executes the following algorithm:

Fetch all objects of type Bar for which the “foreign_key” field equals “a_foo”, and put the resulting objects in a field called “other” on the returned Foo objects

0 Likes