Skip to content

Commit

Permalink
fix: adding property setter for table constraints, googleapis#1990
Browse files Browse the repository at this point in the history
  • Loading branch information
lkhagvadorj-amp committed Dec 20, 2024
1 parent 3359ef3 commit fd5fbd0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
37 changes: 36 additions & 1 deletion google/cloud/bigquery/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,18 @@ def table_constraints(self) -> Optional["TableConstraints"]:
table_constraints = TableConstraints.from_api_repr(table_constraints)
return table_constraints

@table_constraints.setter
def table_constraints(self, value):
"""Tables Primary Key and Foreign Key information."""
api_repr = value
if isinstance(value, TableConstraints):
api_repr = value.to_api_repr()
elif value is not None:
raise ValueError(
"value must be google.cloud.bigquery.table.TableConstraints " "or None"
)
self._properties[self._PROPERTY_TO_API_FIELD["table_constraints"]] = api_repr

@classmethod
def from_string(cls, full_table_id: str) -> "Table":
"""Construct a table from fully-qualified table ID.
Expand Down Expand Up @@ -3247,7 +3259,20 @@ def from_api_repr(cls, api_repr: Dict[str, Any]) -> "ForeignKey":
for column_reference_resource in api_repr["columnReferences"]
],
)


def to_api_repr(self) -> Dict[str, Any]:
"""Return a dictionary representing this object."""
return {
"name": self.name,
"referencedTable": self.referenced_table.to_api_repr(),
"columnReferences": [
{
"referencingColumn": column_reference.referencing_column,
"referencedColumn": column_reference.referenced_column,
}
for column_reference in self.column_references
],
}

class TableConstraints:
"""The TableConstraints defines the primary key and foreign key.
Expand Down Expand Up @@ -3284,6 +3309,16 @@ def from_api_repr(cls, resource: Dict[str, Any]) -> "TableConstraints":
]
return cls(primary_key, foreign_keys)

def to_api_repr(self) -> Dict[str, Any]:
"""Return a dictionary representing this object."""
resource = {}
if self.primary_key:
resource["primaryKey"] = {"columns": self.primary_key.columns}
if self.foreign_keys:
resource["foreignKeys"] = [
foreign_key.to_api_repr() for foreign_key in self.foreign_keys
]
return resource

def _item_to_row(iterator, resource):
"""Convert a JSON row to the native object.
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,20 @@ def test_table_constraints_property_getter(self):
assert isinstance(table_constraints, TableConstraints)
assert table_constraints.primary_key == PrimaryKey(columns=["id"])

def test_table_constraints_property_setter(self):
from google.cloud.bigquery.table import PrimaryKey, TableConstraints

dataset = DatasetReference(self.PROJECT, self.DS_ID)
table_ref = dataset.table(self.TABLE_NAME)
table = self._make_one(table_ref)

table_constraints = TableConstraints(primary_key=PrimaryKey(columns=["id"]), foreign_keys=[])
table.table_constraints = table_constraints

assert table._properties["tableConstraints"] == {
"primaryKey": {"columns": ["id"]},
}

def test_description_setter_bad_value(self):
dataset = DatasetReference(self.PROJECT, self.DS_ID)
table_ref = dataset.table(self.TABLE_NAME)
Expand Down

0 comments on commit fd5fbd0

Please sign in to comment.