Skip to content

schema built when creating a view in bigquery #1701

@jeremyjamesmitchell

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_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, 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},
        }
        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

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