Calling create on a table with view_query set fails, as _build_resource sets the schema. Running
new_table=dataset.table('test_view')
table.view_query=query_stringnew_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, e.g.:
def_build_resource(self):
"""Generate a resource for ``create`` or ``update``."""resource= {
'tableReference': {
'projectId': self._dataset.project,
'datasetId': self._dataset.name,
'tableId': self.name},
}
ifself.descriptionisnotNone:
resource['description'] =self.descriptionifself.expiresisnotNone:
value=_millis_from_datetime(self.expires)
resource['expirationTime'] =valueifself.friendly_nameisnotNone:
resource['friendlyName'] =self.friendly_nameifself.locationisnotNone:
resource['location'] =self.locationifself.view_queryisnotNone:
view=resource['view'] = {}
view['query'] =self.view_queryelse:
resource['schema'] = {'fields': _build_schema_resource(self._schema)}
returnresource
Calling create on a table with view_query set fails, as _build_resource sets the schema. Running
gives the following error:
One fix that works is only setting
schemaifview_queryisNonein_build_resourceingcloud/bigquery/table.py, e.g.: