From f806e5b2d15aeb25574a726396dac336ed94daf3 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Thu, 7 Apr 2016 12:16:54 -0400 Subject: [PATCH 1/3] Omit 'schema' as part of 'Table.{create,update}' when 'view_query is set. Closes #1701. --- gcloud/bigquery/table.py | 2 ++ gcloud/bigquery/test_table.py | 11 +---------- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/gcloud/bigquery/table.py b/gcloud/bigquery/table.py index bbe1f1ccfdf9..15f429ff15be 100644 --- a/gcloud/bigquery/table.py +++ b/gcloud/bigquery/table.py @@ -419,6 +419,8 @@ def _build_resource(self): if self.view_query is not None: view = resource['view'] = {} view['query'] = self.view_query + # https://github.com/GoogleCloudPlatform/gcloud-python/issues/1701 + del resource['schema'] return resource diff --git a/gcloud/bigquery/test_table.py b/gcloud/bigquery/test_table.py index 2069d2455fc2..16ce4de73334 100644 --- a/gcloud/bigquery/test_table.py +++ b/gcloud/bigquery/test_table.py @@ -511,9 +511,6 @@ def test_create_w_alternate_client(self): 'projectId': self.PROJECT, 'datasetId': self.DS_NAME, 'tableId': self.TABLE_NAME}, - 'schema': {'fields': [ - {'name': 'full_name', 'type': 'STRING', 'mode': 'REQUIRED'}, - {'name': 'age', 'type': 'INTEGER', 'mode': 'REQUIRED'}]}, 'description': DESCRIPTION, 'friendlyName': TITLE, 'view': {'query': QUERY}, @@ -800,10 +797,7 @@ def test_update_w_alternate_client(self): conn2 = _Connection(RESOURCE) client2 = _Client(project=self.PROJECT, connection=conn2) dataset = _Dataset(client1) - full_name = SchemaField('full_name', 'STRING', mode='REQUIRED') - age = SchemaField('age', 'INTEGER', mode='REQUIRED') - table = self._makeOne(self.TABLE_NAME, dataset=dataset, - schema=[full_name, age]) + table = self._makeOne(self.TABLE_NAME, dataset=dataset) table.default_table_expiration_ms = DEF_TABLE_EXP table.location = LOCATION table.expires = self.EXP_TIME @@ -821,9 +815,6 @@ def test_update_w_alternate_client(self): {'projectId': self.PROJECT, 'datasetId': self.DS_NAME, 'tableId': self.TABLE_NAME}, - 'schema': {'fields': [ - {'name': 'full_name', 'type': 'STRING', 'mode': 'REQUIRED'}, - {'name': 'age', 'type': 'INTEGER', 'mode': 'REQUIRED'}]}, 'expirationTime': _millis(self.EXP_TIME), 'location': 'EU', 'view': {'query': QUERY}, From 06a1fb233ed623b3c394191bba7a149665018c6b Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Thu, 7 Apr 2016 14:15:22 -0400 Subject: [PATCH 2/3] Adjust comment about omitting schema when 'view_query' is set. Addresses: https://github.com/GoogleCloudPlatform/gcloud-python/pull/1703#discussion_r58904697 --- gcloud/bigquery/table.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gcloud/bigquery/table.py b/gcloud/bigquery/table.py index 15f429ff15be..7e3e18f3f59b 100644 --- a/gcloud/bigquery/table.py +++ b/gcloud/bigquery/table.py @@ -419,7 +419,7 @@ def _build_resource(self): if self.view_query is not None: view = resource['view'] = {} view['query'] = self.view_query - # https://github.com/GoogleCloudPlatform/gcloud-python/issues/1701 + # Back-end rejects 'schema' when passing a query. del resource['schema'] return resource From f755f0a9afdd38f52f0a0cb05162b2ea9150e10f Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Thu, 7 Apr 2016 15:14:32 -0400 Subject: [PATCH 3/3] Wait to add schema until after checking for 'view_query'. Addresses: https://github.com/GoogleCloudPlatform/gcloud-python/pull/1703#discussion_r58925772. --- gcloud/bigquery/table.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/gcloud/bigquery/table.py b/gcloud/bigquery/table.py index 7e3e18f3f59b..07da7e34e2e3 100644 --- a/gcloud/bigquery/table.py +++ b/gcloud/bigquery/table.py @@ -401,7 +401,6 @@ def _build_resource(self): 'projectId': self._dataset.project, 'datasetId': self._dataset.name, 'tableId': self.name}, - 'schema': {'fields': _build_schema_resource(self._schema)}, } if self.description is not None: resource['description'] = self.description @@ -419,8 +418,10 @@ def _build_resource(self): if self.view_query is not None: view = resource['view'] = {} view['query'] = self.view_query - # Back-end rejects 'schema' when passing a query. - del resource['schema'] + elif self._schema: + resource['schema'] = { + 'fields': _build_schema_resource(self._schema) + } return resource