Skip to content

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

Closed

Description

@happyhuman
  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!

Activity

  1. ryanyuan commented on Jul 18, 2019

    @ryanyuan
    Contributor

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

  2. tseaver commented on Jul 18, 2019

    @tseaver
    Contributor

    @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

  3. philippabaliman commented on Aug 5, 2020

    @philippabaliman

    Still having the same issue on v1.26.1

  4. ryanyuan commented on Aug 7, 2020

    @ryanyuan
    Contributor

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

  5. ikendoit commented on Nov 28, 2020

    @ikendoit

    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.

  6. nlarge-google commented on Nov 17, 2022

    @nlarge-google

    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

Metadata

Metadata

Assignees

Labels

api: bigqueryIssues related to the BigQuery API.status: invalidtype: 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