| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,14 +55,14 @@ def ddl(self) -> str: | |||
| 55 | 55 | ] | |
| 56 | 56 | key_fields_ddl = ', '.join(key_fields) | |
| 57 | 57 | for relation in self._model.foreign_key_relations.values(): | |
| 58 | - for referencing_table_col, referenced_table_col in relation.constraints.items(): | ||
| 58 | + for constraint in relation.constraints: | ||
| 59 | 59 | key_fields_ddl += ( | |
| 60 | - ', FOREIGN KEY ({referencing_table_col}) REFERENCES' | ||
| 61 | - ' {parent} ({referenced_table_col})').format( | ||
| 62 | - parent=relation.destination, | ||
| 63 | - referencing_table_col=referencing_table_col, | ||
| 64 | - referenced_table_col=referenced_table_col, | ||
| 65 | - ) | ||
| 60 | + ', FOREIGN KEY ({referencing_column}) REFERENCES' | ||
| 61 | + ' {referenced_table} ({referenced_column})').format( | ||
| 62 | + referencing_column=constraint.referencing_column, | ||
| 63 | + referenced_table=constraint.referenced_table_name, | ||
| 64 | + referenced_column=constraint.referenced_column, | ||
| 65 | + ) | ||
| 66 | 66 | index_ddl = 'PRIMARY KEY ({})'.format(', '.join(self._model.primary_keys)) | |
| 67 | 67 | statement = 'CREATE TABLE {} ({}) {}'.format(self._model.table, | |
| 68 | 68 | key_fields_ddl, index_ddl) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,19 +14,17 @@ | |||
| 14 | 14 | # limitations under the License. | |
| 15 | 15 | """Helps define a foreign key relationship between two models.""" | |
| 16 | 16 | ||
| 17 | - from typing import Any, List, Mapping, Type, Union | ||
| 17 | + from typing import List, Mapping | ||
| 18 | 18 | ||
| 19 | 19 | import dataclasses | |
| 20 | - from spanner_orm import error | ||
| 21 | 20 | from spanner_orm import registry | |
| 22 | 21 | ||
| 23 | 22 | ||
| 24 | 23 | @dataclasses.dataclass | |
| 25 | 24 | class ForeignKeyRelationshipConstraint: | |
| 26 | 25 | referencing_column: str | |
| 27 | - referenced_columns: str | ||
| 26 | + referenced_column: str | ||
| 28 | 27 | referenced_table_name: str | |
| 29 | - | ||
| 30 | 28 | ||
| 31 | 29 | ||
| 32 | 30 | class ForeignKeyRelationship(object): | |
@@ -38,8 +36,7 @@ def __init__(self, | |||
| 38 | 36 | """Creates a ForeignKeyRelationship. | |
| 39 | 37 | ||
| 40 | 38 | Args: | |
| 41 | - referenced_table_name: Destination model class or fully qualified class | ||
| 42 | - name of the destination model. | ||
| 39 | + referenced_table_name: Name of the table which the foreign key references. | ||
| 43 | 40 | constraints: Dictionary where the keys are names of columns from the | |
| 44 | 41 | referencing table and the values are the names of the columns in the | |
| 45 | 42 | referenced table. | |
@@ -52,31 +49,20 @@ def __init__(self, | |||
| 52 | 49 | ||
| 53 | 50 | @property | |
| 54 | 51 | def constraints(self) -> List[ForeignKeyRelationshipConstraint]: | |
| 55 | - return self._constraints | ||
| 56 | - | ||
| 57 | - @property | ||
| 58 | - def destination(self) -> Type[Any]: | ||
| 59 | - return registry.model_registry().get(self._referenced_table_name).table | ||
| 60 | - if not self._destination: | ||
| 61 | - self._destination = registry.model_registry().get( | ||
| 62 | - self._referenced_table_name) | ||
| 63 | - return self._destination | ||
| 52 | + return self._parse_constraints() | ||
| 64 | 53 | ||
| 65 | 54 | def _parse_constraints(self) -> List[ForeignKeyRelationshipConstraint]: | |
| 66 | - """Validates the dictionary of constraints and turns it into Conditions.""" | ||
| 55 | + """Returns a list of Constraints for the relationship.""" | ||
| 67 | 56 | constraints = [] | |
| 68 | - for origin_column, destination_column in self._constraints.items(): | ||
| 69 | - if origin_column not in self.origin.fields: | ||
| 70 | - raise error.ValidationError( | ||
| 71 | - 'Origin column must be present in origin model') | ||
| 72 | - | ||
| 73 | - if destination_column not in self.destination.fields: | ||
| 74 | - raise error.ValidationError( | ||
| 75 | - 'Destination column must be present in destination model') | ||
| 76 | - | ||
| 77 | - # TODO(dbrandao): remove when pytype #234 is fixed | ||
| 57 | + referenced_table = registry.model_registry().get( | ||
| 58 | + self._referenced_table_name) | ||
| 59 | + for referencing_column, referenced_column in self._constraints.items(): | ||
| 78 | 60 | constraints.append( | |
| 79 | - RelationshipConstraint(self.destination, destination_column, | ||
| 80 | - self.origin, origin_column)) # type: ignore | ||
| 61 | + ForeignKeyRelationshipConstraint( | ||
| 62 | + referencing_column, | ||
| 63 | + referenced_column, | ||
| 64 | + referenced_table.table, | ||
| 65 | + ) | ||
| 66 | + ) | ||
| 81 | 67 | ||
| 82 | 68 | return constraints | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -45,7 +45,11 @@ def __init__(self, | |||
| 45 | 45 | table: Optional[str] = None, | |
| 46 | 46 | fields: Optional[Dict[str, field.Field]] = None, | |
| 47 | 47 | relations: Optional[Dict[str, relationship.Relationship]] = None, | |
| 48 | - foreign_key_relations: Optional[Dict[str, foreign_key_relationship.ForeignKeyRelationship]] = None, | ||
| 48 | + foreign_key_relations: Optional[ | ||
| 49 | + Dict[ | ||
| 50 | + str, | ||
| 51 | + foreign_key_relationship.ForeignKeyRelationship] | ||
| 52 | + ] = None, | ||
| 49 | 53 | indexes: Optional[Dict[str, index.Index]] = None, | |
| 50 | 54 | interleaved: Optional[str] = None, | |
| 51 | 55 | model_class: Optional[Type[Any]] = None): | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -53,8 +53,6 @@ def __new__(mcs, name: str, bases: Any, attrs: Dict[str, Any], **kwargs: Any): | |||
| 53 | 53 | model_metadata.table = value | |
| 54 | 54 | elif key == '__interleaved__': | |
| 55 | 55 | model_metadata.interleaved = value | |
| 56 | - elif key == '__foreign_key__': | ||
| 57 | - model_metadata.foreign_key = value | ||
| 58 | 56 | if isinstance(value, field.Field): | |
| 59 | 57 | model_metadata.add_field(key, value) | |
| 60 | 58 | elif isinstance(value, index.Index): | |
@@ -121,11 +119,9 @@ def relations(cls) -> Dict[str, relationship.Relationship]: | |||
| 121 | 119 | return cls.meta.relations | |
| 122 | 120 | ||
| 123 | 121 | @property | |
| 124 | - def foreign_key_relations(cls) -> Dict[str, foreign_key_relationship.ForeignKeyRelationship]: | ||
| 122 | + def foreign_key_relations( | ||
| 123 | + cls) -> Dict[str, foreign_key_relationship.ForeignKeyRelationship]: | ||
| 125 | 124 | return cls.meta.foreign_key_relations | |
| 126 | - #if cls.meta.foreign_key: | ||
| 127 | - # return registry.model_registry().get(cls.meta.foreign_key) | ||
| 128 | - #return None | ||
| 129 | 125 | ||
| 130 | 126 | ||
| 131 | 127 | @property | |
| Back | FazBrowse Home | New Git URL |
0 commit comments