-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add create method to relations (#244)
Changes: - add create method to relations - add more documentation related to one to many relations - add tests for create method
- Loading branch information
Showing
10 changed files
with
159 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# Many-to-One relations | ||
|
||
Many to one relations are the inverse of a `ForeignKey`. There is only an implicit field for this, | ||
which is added to the target model with the related name specified or automatically generated. | ||
The interface is quite similar to [ManyToMany](./many-to-many.md). | ||
|
||
|
||
## Operations | ||
|
||
With the many to many you can perform all the normal operations of searching from normal queries | ||
to the [related_name][related_name] as per normal search. | ||
|
||
ManyToMany allows three different methods when using it (the same applies for the reverse side). | ||
|
||
* `add()` - Adds a record to the relation (Updates the ForeignKey). | ||
* `create()` - Create a new record and add it to the relation. | ||
* `remove()` - Removes a record to the relation (set the ForeignKey to None). | ||
|
||
Let us see how it looks by using the following example. | ||
|
||
```python+ | ||
{!> ../docs_src/queries/manytoone/example.py !} | ||
``` | ||
|
||
#### add() | ||
|
||
You can now add teams to organisations, something like this. | ||
|
||
```python | ||
member = await TeamMember.query.create(name="member1") | ||
blue_team = await Team.query.create(name="Blue Team")´ | ||
|
||
await blue_team.members.add(member) | ||
``` | ||
|
||
#### create() | ||
|
||
You can fuse this to: | ||
|
||
|
||
```python | ||
blue_team = await Team.query.create(name="Blue Team") | ||
green_team = await Team.query.create(name="Green Team") | ||
member1 = await blue_team.members.create(name="edgy") | ||
member2 = await green_team.members.create(name="fastapi") | ||
``` | ||
|
||
This is also more performant because less transactions are required. | ||
|
||
#### remove() | ||
|
||
You can now remove teams from organisations, something like this. | ||
|
||
```python | ||
blue_team = await Team.query.create(name="Blue Team")´ | ||
|
||
member = await blue_team.members.create(name="member1") | ||
# and now remove | ||
await blue_team.members.remove(member) | ||
``` | ||
|
||
Hint: when unique, remove works also without argument. | ||
|
||
#### Related name | ||
|
||
When a [related_name][related_name] is not defined, Edgy will automatically generate one with the following | ||
format: | ||
|
||
```shell | ||
<foreignkey>s_set | ||
``` | ||
|
||
|
||
[related_name]: ./related-name.md |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
from typing import List | ||
|
||
import edgy | ||
from edgy import Database, Registry | ||
|
||
database = Database("sqlite:///db.sqlite") | ||
models = Registry(database=database) | ||
|
||
|
||
class Team(edgy.Model): | ||
name: str = edgy.fields.CharField(max_length=100) | ||
|
||
class Meta: | ||
registry = models | ||
|
||
|
||
class TeamMember(edgy.Model): | ||
name: str = edgy.fields.CharField(max_length=100) | ||
team: Team = edgy.fields.ForeignKey(Team, related_name="members") | ||
|
||
class Meta: | ||
registry = models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters