Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SQLAlchemy 1.4 tutorial
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
fconil small programs
Python
SQLAlchemy
SQLAlchemy 1.4 tutorial
Commits
9eafc1d7
Commit
9eafc1d7
authored
3 years ago
by
Françoise Conil
Browse files
Options
Downloads
Patches
Plain Diff
Code complété pour mieux visualiser le résultat (__repr__)
parent
7412d818
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
relationship/one_to_many.2.py
+24
-2
24 additions, 2 deletions
relationship/one_to_many.2.py
with
24 additions
and
2 deletions
relationship/one_to_many.2.py
+
24
−
2
View file @
9eafc1d7
...
...
@@ -6,7 +6,7 @@ many to one, specify an additional relationship() and connect the two using the
relationship.back_populates parameter.
"""
from
sqlalchemy
import
create_engine
from
sqlalchemy
import
Column
,
ForeignKey
,
Integer
from
sqlalchemy
import
Column
,
ForeignKey
,
Integer
,
String
from
sqlalchemy.orm
import
declarative_base
,
relationship
from
sqlalchemy.orm.session
import
Session
...
...
@@ -17,17 +17,25 @@ class Parent(Base):
__tablename__
=
"
parent
"
id
=
Column
(
Integer
,
primary_key
=
True
)
name
=
Column
(
String
)
children
=
relationship
(
"
Child
"
,
back_populates
=
"
parent
"
)
def
__repr__
(
self
):
return
f
"
<Parent (name=
{
self
.
name
}
, children=
{
self
.
children
}
)>
"
class
Child
(
Base
):
__tablename__
=
"
child
"
id
=
Column
(
Integer
,
primary_key
=
True
)
name
=
Column
(
String
)
parent_id
=
Column
(
Integer
,
ForeignKey
(
"
parent.id
"
))
parent
=
relationship
(
"
Parent
"
,
back_populates
=
"
children
"
)
def
__repr__
(
self
):
return
f
"
<Child (name=
{
self
.
name
}
)>
"
if
__name__
==
"
__main__
"
:
engine
=
create_engine
(
"
sqlite:///one_to_many.2.db
"
,
echo
=
False
)
...
...
@@ -37,5 +45,19 @@ if __name__ == "__main__":
# cf ../basic-session.1.4.py
with
Session
(
engine
)
as
session
:
jack
=
Parent
(
name
=
"
Jack
"
)
alice
=
Child
(
name
=
"
Alice
"
)
john
=
Child
(
name
=
"
John
"
)
jack
.
children
=
[
john
,
alice
]
with
session
.
begin
():
pass
session
.
add
(
jack
)
session
.
add
(
john
)
session
.
add
(
alice
)
print
(
jack
)
# <Parent (name=Jack, children=[<Child (name=John)>, <Child (name=Alice)>])>
print
(
alice
)
# <Child (name=Alice)>
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment