FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-spanner-orm/spanner_orm/foreign_key_relationship.py at main · 7dracoder/python-spanner-orm · GitHub
7dracoder
/
python-spanner-orm
Public
forked from
google/python-spanner-orm
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
python-spanner-orm
/
spanner_orm
/
foreign_key_relationship.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
78 lines (66 loc) · 2.66 KB
Breadcrumbs
python-spanner-orm
/
spanner_orm
/
foreign_key_relationship.py
Copy path
File metadata and controls
78 lines (66 loc) · 2.66 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Helps define a foreign key relationship between two models."""
from
typing
import
Any
,
Mapping
,
Type
import
dataclasses
from
spanner_orm
import
registry
@
dataclasses
.
dataclass
class
ForeignKeyRelationshipConstraint
:
columns
:
Mapping
[
str
,
str
]
referenced_table_name
:
str
referenced_table
:
Type
[
Any
]
class
ForeignKeyRelationship
:
"""Helps define a foreign key relationship between two models."""
def
__init__
(
self
,
referenced_table_name
:
str
,
columns
:
Mapping
[
str
,
str
]):
"""Creates a ForeignKeyRelationship.
Args:
referenced_table_name: Name of the table which the foreign key references.
columns: Dictionary where the keys are names of columns from the
referencing table and the values are the names of the columns in the
referenced table.
"""
self
.
origin
=
None
self
.
name
=
None
self
.
_referenced_table_name
=
referenced_table_name
self
.
_columns
=
columns
@
property
def
constraint
(
self
)
->
ForeignKeyRelationshipConstraint
:
return
self
.
_parse_constraint
()
@
property
def
ddl
(
self
)
->
str
:
referencing_columns_ddl
=
', '
.
join
(
self
.
constraint
.
columns
.
keys
())
referenced_columns_ddl
=
', '
.
join
(
self
.
constraint
.
columns
.
values
())
return
(
'CONSTRAINT {fk_name} FOREIGN KEY ({referencing_columns}) REFERENCES'
' {referenced_table} ({referenced_columns})'
).
format
(
fk_name
=
self
.
name
,
referencing_columns
=
referencing_columns_ddl
,
referenced_table
=
self
.
constraint
.
referenced_table_name
,
referenced_columns
=
referenced_columns_ddl
,
)
def
_parse_constraint
(
self
)
->
ForeignKeyRelationshipConstraint
:
"""Return the relationship constraint."""
referenced_table
=
registry
.
model_registry
().
get
(
self
.
_referenced_table_name
)
return
ForeignKeyRelationshipConstraint
(
self
.
_columns
,
referenced_table
.
table
,
referenced_table
,
)
@
property
def
single
(
self
)
->
bool
:
# Spanner enforces uniqueness for values of fields referenced by
# foreign keys, because it creates a unique index on the referenced
# key.
return
True
Back
|
FazBrowse Home
|
New Git URL