Repository: googleapis/google-cloud-python

Issue:
schema built when creating a view in bigquery
Calling create on a table with view_query set fails, as _build_resource sets the schema. Running

``` python
new_table = dataset.table('test_view')
table.view_query = query_string
new_table.create()
```

gives the following error:

```
BadRequest: 400 {
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "invalid",
    "message": "Schema field shouldn't be used as input with a view"
   }
  ],
  "code": 400,
  "message": "Schema field shouldn't be used as input with a view"
 }
}
```

One fix that works is only setting `schema` if `view_query` is `None` in `_build_resource` in [`gcloud/bigquery/table.py`](https://github.com/GoogleCloudPlatform/gcloud-python/blob/188515f192e3823ce21a47df2d4742995d08f109/gcloud/bigquery/table.py#L397), e.g.:

``` python
    def _build_resource(self):
        """Generate a resource for ``create`` or ``update``."""
        resource = {
            'tableReference': {
                'projectId': self._dataset.project,
                'datasetId': self._dataset.name,
                'tableId': self.name},
        }
        if self.description is not None:
            resource['description'] = self.description

        if self.expires is not None:
            value = _millis_from_datetime(self.expires)
            resource['expirationTime'] = value

        if self.friendly_name is not None:
            resource['friendlyName'] = self.friendly_name

        if self.location is not None:
            resource['location'] = self.location

        if self.view_query is not None:
            view = resource['view'] = {}
            view['query'] = self.view_query
        else:
            resource['schema'] = {'fields': _build_schema_resource(self._schema)}

        return resource
```



Discussion:
Thanks for the detailed report!


Resolving patch:
diff --git a/gcloud/bigquery/table.py b/gcloud/bigquery/table.py
--- 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,6 +418,10 @@ def _build_resource(self):
         if self.view_query is not None:
             view = resource['view'] = {}
             view['query'] = self.view_query
+        elif self._schema:
+            resource['schema'] = {
+                'fields': _build_schema_resource(self._schema)
+            }
 
         return resource
 


Test patch:
diff --git a/gcloud/bigquery/test_table.py b/gcloud/bigquery/test_table.py
--- 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},