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
da2bdb1f
Commit
da2bdb1f
authored
3 years ago
by
Françoise Conil
Browse files
Options
Downloads
Patches
Plain Diff
Test d'une association de 3 tables : pas abouti
parent
20e426c0
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/association_object_ternary.py
+101
-0
101 additions, 0 deletions
relationship/association_object_ternary.py
with
101 additions
and
0 deletions
relationship/association_object_ternary.py
0 → 100644
+
101
−
0
View file @
da2bdb1f
"""
https://docs.sqlalchemy.org/en/14/orm/basic_relationships.html#association-object
"""
from
datetime
import
datetime
from
sqlalchemy
import
create_engine
from
sqlalchemy
import
Column
,
ForeignKey
,
Integer
,
Text
,
DateTime
from
sqlalchemy.orm
import
declarative_base
,
relationship
from
sqlalchemy.orm.session
import
Session
Base
=
declarative_base
()
class
UserOrgRole
(
Base
):
__tablename__
=
"
user_org_role
"
user_id
=
Column
(
ForeignKey
(
"
user.id
"
,
ondelete
=
"
CASCADE
"
),
primary_key
=
True
)
org_id
=
Column
(
ForeignKey
(
"
org.id
"
,
ondelete
=
"
CASCADE
"
),
primary_key
=
True
)
# Does it make sense ?
role_id
=
Column
(
ForeignKey
(
"
role.id
"
,
ondelete
=
"
CASCADE
"
),
primary_key
=
True
)
# I do not know what the relationship could back populate, org or role ???
user
=
relationship
(
"
User
"
)
org
=
relationship
(
"
Org
"
)
role
=
relationship
(
"
Role
"
)
class
User
(
Base
):
__tablename__
=
"
user
"
id
=
Column
(
Integer
,
primary_key
=
True
)
username
=
Column
(
Text
,
index
=
True
,
nullable
=
False
)
fullname
=
Column
(
Text
,
nullable
=
False
)
account_type
=
Column
(
Text
,
nullable
=
False
)
def
__repr__
(
self
):
return
(
f
"
<User (username=
{
self
.
username
}
, fullname=
{
self
.
fullname
}
,
"
f
"
account_type=
{
self
.
account_type
}
)>
"
)
class
Org
(
Base
):
__tablename__
=
"
org
"
id
=
Column
(
Integer
,
primary_key
=
True
)
name
=
Column
(
Text
,
index
=
True
,
nullable
=
False
)
slug
=
Column
(
Text
,
index
=
True
,
nullable
=
False
)
created_at
=
Column
(
DateTime
)
def
__repr__
(
self
):
return
(
f
"
<Org (name=
{
self
.
name
}
, slug=
{
self
.
slug
}
,
"
f
"
created_at=
{
self
.
created_at
}
)>
"
)
class
Role
(
Base
):
__tablename__
=
"
role
"
id
=
Column
(
Integer
,
primary_key
=
True
)
name
=
Column
(
Text
,
index
=
True
,
nullable
=
False
)
def
__repr__
(
self
):
return
f
"
<Role (name=
{
self
.
name
}
)>
"
if
__name__
==
"
__main__
"
:
engine
=
create_engine
(
"
sqlite:///association_object_ternary.db
"
,
echo
=
False
)
Base
.
metadata
.
drop_all
(
engine
)
Base
.
metadata
.
create_all
(
engine
)
with
Session
(
engine
)
as
session
:
# create parent, append a child via association
u1
=
User
(
username
=
"
jlondon
"
,
fullname
=
"
Jack London
"
,
account_type
=
"
member
"
,
)
o1
=
Org
(
name
=
"
o1
"
,
slug
=
"
o1
"
,
created_at
=
datetime
.
utcnow
())
owner
=
Role
(
name
=
"
owner
"
)
uor1
=
UserOrgRole
()
uor1
.
user
=
u1
uor1
.
org
=
o1
uor1
.
role
=
owner
with
session
.
begin
():
session
.
add
(
u1
)
session
.
add
(
o1
)
session
.
add
(
owner
)
session
.
add
(
uor1
)
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