1

I have a requirement wherein I need to fetch metadata (table structure) from one of the BigQuery tables and, on that basis, create new tables. For example, table 'Metadata' contains some table structures.

In my Python script FileSubtype is passed dynamically from other script and on basis of it, column names and their corresponding datatypes would be fetched. And new tables will be created with these columns and datatypes.

Stuck part is, all these stuff has to be dynamic.

sample code (for testing purpose i have hardcoded the table name):

query1 = client.run_sync_query('%s limit 10' % QUERY)
query1.run()
rows = list(query1.fetch_data())
table = dataset.table('Sample_BQ_Table1')
for row in rows:
        print(row[0])
# Set the table schema
        table.schema = (
            bigquery.SchemaField(row[0], 'STRING'),
            )
table.create()

I am facing the error

TypeError: 'SchemaField' object is not iterable`

while setting table schema.

James Z
  • 12,209
  • 10
  • 24
  • 44
GaurangR
  • 59
  • 1
  • 9
  • Actually I am getting below error while executing my python script : **TypeError: 'SchemaField' object is not iterable** – GaurangR Aug 01 '17 at 05:56
  • 1
    Are you sure you have the trailing comma after `bigquery.SchemaField(...)` in your code? The error message indicates that you do not. – Tim Swast Aug 02 '17 at 15:01

1 Answers1

3

The table.schema field expects an iterable, such as a list or tuple.

The code

table.schema = (
    bigquery.SchemaField(row[0], 'STRING')
)

would not set schema as an iterable, because plain parentheses don't create a tuple. The correct code would be

table.schema = (
    bigquery.SchemaField(row[0], 'STRING'),
)

Note the trailing comma.

Tim Swast
  • 14,091
  • 4
  • 38
  • 61