I recently updated the google-cloud-bigquery python package and I am getting a KeyError when trying to insert a row with a missing value into a table with a nullable field.
In previous version (I tested it on version 1.5.0) the missing field was automatically filled with a null value.
Environment details
OS: Ubuntu 18.04
Python version:
$ python --version
Python 3.6.7
Package version:
$ pip show google-cloud-bigquery
Name: google-cloud-bigquery
Version: 1.9.0
Note: It works on version 1.5.0
Steps to reproduce
- Create table with nullable field
- Insert row with missing value (provide row as dictionary, with a missing key:value)
Code example
from google.cloud import bigquery
client = bigquery.Client(project='my_project')
dataset_ref = client.dataset('my_dataset')
schema = [
bigquery.SchemaField("user_id", "STRING", mode="REQUIRED"),
bigquery.SchemaField("name", "STRING", mode="NULLABLE"), # NULLABLE
]
table_ref = dataset_ref.table("my_table")
table = bigquery.Table(table_ref, schema=schema)
table = client.create_table(table)
rows_to_insert = [
{'user_id': 1111, 'name': 'peter'}, # OK
{'user_id': 2222, 'name': 'anna'} # OK
]
errors = client.insert_rows(table, rows_to_insert)
assert errors == []
rows_to_insert = [
{'user_id': 3333, 'name': 'john'},
{'user_id': 4444} # FAILS
]
errors = client.insert_rows(table, rows_to_insert) # FAILS HERE
assert errors == []
Stack trace
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-22-a6f1bda68730> in <module>
26 ]
27
---> 28 errors = client.insert_rows(table, rows_to_insert) # FAILS HERE
29 assert errors == []
~/.local/lib/python3.6/site-packages/google/cloud/bigquery/client.py in insert_rows(self, table, rows, selected_fields, **kwargs)
1495 raise TypeError("table should be Table or TableReference")
1496
-> 1497 json_rows = [_record_field_to_json(schema, row) for row in rows]
1498
1499 return self.insert_rows_json(table, json_rows, **kwargs)
~/.local/lib/python3.6/site-packages/google/cloud/bigquery/client.py in <listcomp>(.0)
1495 raise TypeError("table should be Table or TableReference")
1496
-> 1497 json_rows = [_record_field_to_json(schema, row) for row in rows]
1498
1499 return self.insert_rows_json(table, json_rows, **kwargs)
~/.local/lib/python3.6/site-packages/google/cloud/bigquery/_helpers.py in _record_field_to_json(fields, row_value)
399 for subindex, subfield in enumerate(fields):
400 subname = subfield.name
--> 401 subvalue = row_value[subname] if isdict else row_value[subindex]
402 record[subname] = _field_to_json(subfield, subvalue)
403 return record
KeyError: 'name'
I recently updated the google-cloud-bigquery python package and I am getting a KeyError when trying to insert a row with a missing value into a table with a nullable field.
In previous version (I tested it on version 1.5.0) the missing field was automatically filled with a null value.
Environment details
OS: Ubuntu 18.04
Python version:
Package version:
Note: It works on version 1.5.0
Steps to reproduce
Code example
Stack trace