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

BigQuery: The sample to create a BigQuery table is not working #8703

Closed
happyhuman opened this issue Jul 17, 2019 · 6 comments
Closed

BigQuery: The sample to create a BigQuery table is not working #8703

happyhuman opened this issue Jul 17, 2019 · 6 comments
Assignees
Labels
api: bigquery Issues related to the BigQuery API. status: invalid type: docs Improvement to the documentation for an API.

Comments

@happyhuman
Copy link
Contributor

happyhuman commented Jul 17, 2019

  1. Specify the API at the beginning of the title (for example, "BigQuery: ...")
    General, Core, and Other are also allowed as types
    This sample to be exact: https://github.com/googleapis/google-cloud-python/blob/7ba0220ff9d0d68241baa863b3152c34dc9f7a1a/bigquery/samples/create_table.py

  2. OS type and version
    MacOS Mojave

  3. Python version and virtual environment information: python --version
    Both 2.7.10 and 3.7

  4. google-cloud- version: pip show google-<service> or pip freeze
    google-cloud-bigquery==1.9.0

Steps to reproduce

  1. set GOOGLE_APPLICATION_CREDENTIALS to my service account key
  2. Make sure "gcloud config list" items are correct.
  3. In the sample code, set table_id to: "{}.datalabeling.cont_eval".format(PROJECT_ID)
  4. Run the sample.

Code example

def create_big_query_table():
    schema = [
        bigquery.SchemaField("model", "STRING", mode="REQUIRED"),
        bigquery.SchemaField("model_version", "INTEGER", mode="REQUIRED"),
    ]

    client = bigquery.Client()
    PROJECT_ID = 'my-gcp-project-id'
    table_id = "{}.datalabeling.cont_eval".format(PROJECT_ID)
    table = bigquery.Table(table_id, schema=schema)
    table = client.create_table(table)  # API request
    print(table)

Stack trace

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/shahins/.virtualenvs/dl2/lib/python2.7/site-packages/google/cloud/bigquery/table.py", line 370, in __init__
    self._properties = {"tableReference": table_ref.to_api_repr(), "labels": {}}
AttributeError: 'str' object has no attribute 'to_api_repr'

(the line that throws this exception is bigquery.Table(.....))

Making sure to follow these steps will guarantee the quickest resolution possible.

Thanks!

@ryanyuan
Copy link
Contributor

I believe this has been fixed since 1.10.0 by having a string check in Table.init().

@tseaver tseaver added api: bigquery Issues related to the BigQuery API. status: invalid type: docs Improvement to the documentation for an API. labels Jul 18, 2019
@tseaver
Copy link
Contributor

tseaver commented Jul 18, 2019

@ryanyuan is correct: the Table constructor has accepted strings formatted in that form since PR #7483 was merged, included in the google-cloud-bigquery 1.10.0 release

@tseaver tseaver closed this as completed Jul 18, 2019
@philippabaliman
Copy link

Still having the same issue on v1.26.1

@ryanyuan
Copy link
Contributor

ryanyuan commented Aug 7, 2020

@philippabaliman No issue for me with google-cloud-bigquery==1.26.1. Can you post your stacktrace here?

@ikendoit
Copy link

check if when you assign a variables like table name, it is an actual string type.
I had this error too, because I had this line:

    table_full_path = "{}.{}".format(dataset_id, table_name),

notice the accidental comma at the end. That would make the variable to be tuple type instead of string.

@nlarge-google
Copy link

This is still an issue, unfortunately. Here is the code that is causing the issue, but with changes to it that I believe fix it, at least in my case. Could someone please take a look at the code and consider this approach as a fix? Without a solution to this issue loading schema files through bigquery.Table will not work with RECORD. I'm not entirely sure about STRUCT, but I assume that would be screened at QA. Thanks...

( from lib/python3.8/site-packages/google/cloud/bigquery/schema.py )

def to_api_repr(self) -> dict:
"""Return a dictionary representing this schema field.

    Returns:
        Dict: A dictionary representing the SchemaField in a serialized form.
    """
    answer = self._properties.copy()

    # If this is a RECORD type, then sub-fields are also included,
    # add this to the serialized representation.
    if self.field_type.upper() in _STRUCT_TYPES:
        # answer["fields"] = [f.to_api_repr() for f in self.fields]
        answer["fields"] = ""
        for f in self.fields: answer["fields"]+=f
        answer["fields"] = list(eval(answer["fields"]))

    # Done; return the serialized dictionary.
    return answer

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. status: invalid type: docs Improvement to the documentation for an API.
Projects
None yet
Development

No branches or pull requests

6 participants