| 7 | | entities. The supported relationship types are as follows: |
| | 7 | entities. Elixir supports the following types of relationships: belongs_to_, |
| | 8 | has_one_, has_many_ and has_and_belongs_to_many_. For all types of |
| | 9 | relationships, you **must** specify the 'kind' of object you are relating to |
| | 10 | using the ``of_kind`` keyword argument. |
| | 11 | |
| | 12 | Additionally, if you want a bidirectionnal relationship, you should define the |
| | 13 | inverse relationship on the other entity explicitely (as opposed to |
| | 14 | SQLAlchemy's backref definitions). In non-ambiguous situations, Elixir will |
| | 15 | match relationships together automatically. If there are several relationships |
| | 16 | of the same type between two entities, Elixir is not able to determine which |
| | 17 | relationship is the inverse of which, so you have to disambiguate the |
| | 18 | situation by giving the name of the inverse relationship in the ``inverse`` |
| | 19 | keyword argument. |
| | 20 | |
| | 21 | Here is a detailed explanation of each relation type: |
| 18 | | belongs_to('owner', of_kind='Person', inverse='pets') |
| 19 | | |
| 20 | | You must specify the 'kind' of object that you are relating to using the |
| 21 | | of_kind keyword argument. Additionally, if you plan on defining the other |
| 22 | | side of the relationship, you should specify the name of the relationship |
| 23 | | on the other side using the 'inverse' keyword argument. |
| 24 | | |
| | 33 | belongs_to('owner', of_kind='Person') |
| 28 | | TODO. |
| 29 | | |
| | 37 | |
| | 38 | Describes the parent's side of a parent-child relationship when there is only |
| | 39 | one child. For example, a `Car` object has one gear stick, which is |
| | 40 | represented as a `GearStick` object. This could be expressed like so: |
| | 41 | |
| | 42 | :: |
| | 43 | |
| | 44 | class Car(Entity): |
| | 45 | has_one('gear_stick', of_kind='GearStick', inverse='car') |
| | 46 | |
| | 47 | class GearStick(Entity): |
| | 48 | belongs_to('car', of_kind='Car') |
| | 49 | |
| | 50 | Note that an ``has_one`` relationship **cannot exist** without a corresponding |
| | 51 | ``belongs_to`` relationship in the other way. |
| 33 | | TODO. |
| 34 | | |
| | 55 | |
| | 56 | Describes the parent's side of a parent-child relationship when there can be |
| | 57 | several children. For example, a `Person` object has many children, each of |
| | 58 | them being a `Person`. This could be expressed like so: |
| | 59 | |
| | 60 | :: |
| | 61 | |
| | 62 | class Person(Entity): |
| | 63 | belongs_to('parent', of_kind='Person') |
| | 64 | has_many('children', of_kind='Person') |
| | 65 | |
| | 66 | Note that an ``has_many`` relationship **cannot exist** without a |
| | 67 | corresponding ``belongs_to`` relationship in the other way. |
| 38 | | TODO. |
| | 71 | |
| | 72 | Describes a relationship in which one kind of entity can be related to several |
| | 73 | objects of the other kind but the objects of that other kind can be related to |
| | 74 | several objects of the first kind. For example, an `Article` can have several |
| | 75 | tags, but the same `Tag` can be used on several articles. |
| | 76 | |
| | 77 | :: |
| | 78 | |
| | 79 | class Article(Entity): |
| | 80 | has_and_belongs_to_many('tags', of_kind='Tag') |
| | 81 | |
| | 82 | class Tag(Entity): |
| | 83 | has_and_belongs_to_many('articles', of_kind='Article') |
| | 84 | |
| | 85 | Note that you don't necessarily need to define the inverse relationship. In |
| | 86 | our example, even though we want tags to be usables on several articles, we |
| | 87 | might not be interested in which articles correspond to a particular tag. In |
| | 88 | that case, we could have omitted the `Tag` side of the relationship. |