diff --git a/bigquery/google/cloud/bigquery/job.py b/bigquery/google/cloud/bigquery/job.py index 084181cdf757..38b3b39c2c3e 100644 --- a/bigquery/google/cloud/bigquery/job.py +++ b/bigquery/google/cloud/bigquery/job.py @@ -1160,6 +1160,10 @@ def schema(self): @schema.setter def schema(self, value): + if value is None: + self._del_sub_prop("schema") + return + if not all(hasattr(field, "to_api_repr") for field in value): raise ValueError("Schema items must be fields") _helpers._set_sub_prop( diff --git a/bigquery/tests/unit/test_job.py b/bigquery/tests/unit/test_job.py index 1e75373c84b6..8bd62d7e4f51 100644 --- a/bigquery/tests/unit/test_job.py +++ b/bigquery/tests/unit/test_job.py @@ -1504,6 +1504,19 @@ def test_schema_setter(self): config._properties["load"]["schema"], {"fields": [full_name_repr, age_repr]} ) + def test_schema_setter_unsetting_schema(self): + from google.cloud.bigquery.schema import SchemaField + + config = self._get_target_class()() + config._properties["load"]["schema"] = [ + SchemaField("full_name", "STRING", mode="REQUIRED"), + SchemaField("age", "INTEGER", mode="REQUIRED"), + ] + + config.schema = None + self.assertNotIn("schema", config._properties["load"]) + config.schema = None # no error, idempotent operation + def test_schema_update_options_missing(self): config = self._get_target_class()() self.assertIsNone(config.schema_update_options)