Skip to content

BigQuery: client.create_table raises Conflict instead of AlreadyExists #123

@jpuig-mind

Description

@jpuig-mind

Environment details

  1. API: BigQuery

  2. OS type and version:

    $ lsb_release -a
    No LSB modules are available.
    Distributor ID: Ubuntu
    Description:    Ubuntu 20.04 LTS
    Release:        20.04
    Codename:       focal
  3. Python version and virtual environment information:

    $ python3 --version
    Python 3.8.2
  4. google-cloud-bigquery version:

    $ pip show google-cloud-bigquery
    Name: google-cloud-bigquery
    Version: 1.24.0
    Summary: Google BigQuery API client library
    Home-page: https://github.com/GoogleCloudPlatform/google-cloud-python
    Author: Google LLC
    Author-email: googleapis-packages@google.com
    License: Apache 2.0
    Location: /home/REDACTED/.local/lib/python3.8/site-packages
    Requires: protobuf, google-api-core, google-cloud-core, google-auth, google-resumable-media, six
    Required-by:

Steps to reproduce

  1. Have or create a table in BigQuery
  2. Try to create it again using client.create_table()
  3. Confirm that the raised expression is google.api_core.exceptions.Conflict
    • One would expect it to be google.api_core.exceptions.AlreadyExists
    • Checked client.delete_table() with a non-existent table and the result is google.api_core.exceptions.NotFound

Code example

Slightly modified version of https://cloud.google.com/bigquery/docs/tables#python

from google.cloud import bigquery
from google.api_core.exceptions import AlreadyExists, Conflict

# Construct a BigQuery client object.
client = bigquery.Client()

# TODO(developer): Set table_id to the ID of the table to create.
table_id = "your-project.your_dataset.your_table_name"

table = bigquery.Table(table_id)
table = client.create_table(table)  # Make an API request.
try:
    table = client.create_table(table)  # Make an API request. Second time.
except AlreadyExists:
    print("Caught google.api_core.exceptions.AlreadyExists")
except Conflict:
    print("Caught google.api_core.exceptions.Conflict")

table = client.create_table(table)  # Make an API request. Third time for Stack Trace

Stack trace

$ /usr/bin/python3 REDACTED/example.py
Caught google.api_core.exceptions.Conflict
Traceback (most recent call last):
  File "REDACTED/example.py", line 19, in <module>
    table = client.create_table(table)  # Make an API request. Third time for Stack Trace
  File "/home/REDACTED/.local/lib/python3.8/site-packages/google/cloud/bigquery/client.py", line 543, in create_table
    api_response = self._call_api(
  File "/home/REDACTED/.local/lib/python3.8/site-packages/google/cloud/bigquery/client.py", line 556, in _call_api
    return call()
  File "/home/REDACTED/.local/lib/python3.8/site-packages/google/api_core/retry.py", line 281, in retry_wrapped_func
    return retry_target(
  File "/home/REDACTED/.local/lib/python3.8/site-packages/google/api_core/retry.py", line 184, in retry_target
    return target()
  File "/home/REDACTED/.local/lib/python3.8/site-packages/google/cloud/_http.py", line 423, in api_request
    raise exceptions.from_http_response(response)
google.api_core.exceptions.Conflict: 409 POST https://bigquery.googleapis.com/bigquery/v2/projects/REDACTED/datasets/REDACTED/tables: Already Exists: Table REDACTED:REDACTED.your_table_name

Metadata

Metadata

Labels

api: bigqueryIssues related to the googleapis/python-bigquery API.type: docsImprovement to the documentation for an API.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions