Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

schema built when creating a view in bigquery #1701

Closed
jeremyjamesmitchell opened this issue Apr 7, 2016 · 1 comment
Closed

schema built when creating a view in bigquery #1701

jeremyjamesmitchell opened this issue Apr 7, 2016 · 1 comment
Assignees
Labels
api: bigquery Issues related to the BigQuery API. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@jeremyjamesmitchell
Copy link

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
@tseaver tseaver added type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. api: bigquery Issues related to the BigQuery API. labels Apr 7, 2016
@tseaver
Copy link
Contributor

tseaver commented Apr 7, 2016

Thanks for the detailed report!

@tseaver tseaver self-assigned this Apr 7, 2016
tseaver added a commit that referenced this issue Apr 8, 2016
… set (#1703)

* Omit 'schema' as part of 'Table.{create,update}' when 'view_query is set.

Closes #1701.

* Adjust comment about omitting schema when 'view_query' is set.

Addresses:
#1703 (comment)

* Wait to add schema until after checking for 'view_query'.

Addresses:
#1703 (comment).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: bigquery Issues related to the BigQuery API. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants