forked from google/python-spanner-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrelationship.py
More file actions
95 lines (80 loc) · 3.27 KB
/
relationship.py
File metadata and controls
95 lines (80 loc) · 3.27 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# python3
# Copyright 2019 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, Dict, List, Type, Union
import dataclasses
from spanner_orm import error
from spanner_orm import registry
@dataclasses.dataclass
class RelationshipConstraint:
destination_class: Type[Any]
destination_column: str
origin_class: Type[Any]
origin_column: str
class Relationship(object):
"""Helps define a foreign key relationship between two models."""
def __init__(self,
destination_handle: Union[Type[Any], str],
constraints: Dict[str, str],
is_parent: bool = False,
single: bool = False):
"""Creates a ModelRelationship.
Args:
destination_handle: Destination model class or fully qualified class name
of the destination model
constraints: Dictionary where the keys are names of columns from the
origin model and the value for a key is the name of the column in the
destination model that the key should be equal to in order for there to
be a relationship from an origin model to the destination
is_parent: True if the destination is a parent table of the origin
single: True if the destination should be treated as a single object
instead of a list of objects
"""
self.origin = None
self.name = None
self._destination_handle = destination_handle
self._destination = None
self._constraints = constraints
self._is_parent = is_parent
self._single = single
@property
def constraints(self) -> List[RelationshipConstraint]:
if not self.origin:
raise error.ValidationError(
'Origin must be set before constraints is called')
return self._parse_constraints()
@property
def destination(self) -> Type[Any]:
if not self._destination:
self._destination = registry.model_registry().get(
self._destination_handle)
return self._destination
@property
def single(self) -> bool:
return self._single
def _parse_constraints(self) -> List[RelationshipConstraint]:
"""Validates the dictionary of constraints and turns it into Conditions."""
constraints = []
for origin_column, destination_column in self._constraints.items():
if origin_column not in self.origin.fields:
raise error.ValidationError(
'Origin column must be present in origin model')
if destination_column not in self.destination.fields:
raise error.ValidationError(
'Destination column must be present in destination model')
constraints.append(
RelationshipConstraint(self.destination, destination_column,
self.origin, origin_column))
return constraints