Skip to content

schema built when creating a view in bigquery #1701

Description

@jeremyjamesmitchell

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

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the BigQuery API.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions