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

Add support for default_value_expression in SchemaField #1398

Closed
JoaoPaes-at-Dynamox opened this issue Nov 3, 2022 · 3 comments
Closed

Add support for default_value_expression in SchemaField #1398

JoaoPaes-at-Dynamox opened this issue Nov 3, 2022 · 3 comments
Assignees
Labels
api: bigquery Issues related to the googleapis/python-bigquery API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.

Comments

@JoaoPaes-at-Dynamox
Copy link

JoaoPaes-at-Dynamox commented Nov 3, 2022

Thanks for stopping by to let us know something could be better!

PLEASE READ: If you have a support contract with Google, please create an issue in the support console instead of filing on GitHub. This will ensure a timely response.

Is your feature request related to a problem? Please describe.
I'm developing a wrapper library to use pydantic.BaseModel as means to define the TableSchema. As part of this wrapper, I would like to provide "server default" values, such as CURRENT_DATETIME, CURRENT_TIMESTAMP, so on, to datetime.datetime fields;

Describe the solution you'd like

client = bigquery.Client("someproject")

bq_table_schema_field = SchemaField(..., default_value_expression="CURRENT_DATETIME()")

table_with_default_value = Table("my_table", schema=[schema_field])

client.create_table(table_with_default_value)

The field "defaultValueExpression" is a valid REST API field and would be a direct mapping from the initializer argument default_value_expression to the API "defaultValueExpression" field.

Describe alternatives you've considered

The alternative to this, would be construct a DDL statement for creating tables which columns can have default values via DEFAULT keyword

base_create = f"CREATE TABLE {somedataset_name}.{table_with_default_value.name} ("
for field in fields:
    field_as_column_statement = f'{field.name} {as_bq_type(field.type)} {"DEFAULT" if field.has_server_default else ""} {field.server_default_statement if field.has_server_default else ""}, '
    base_create = base_create + field_as_column_statement
base_create = base_create + ");"

Which is very cumbersome and ugly, and useless, since the REST API already provides a field for default value expressions.

@product-auto-label product-auto-label bot added the api: bigquery Issues related to the googleapis/python-bigquery API. label Nov 3, 2022
@JoaoPaes-at-Dynamox
Copy link
Author

Actually, a very much simpler, but extremely bad practice, way of solving this is

...

bq_table_schema_field = SchemaField(...)
bq_table_schema_field._properties["defaultValueExpression"] =  "CURRENT_DATETIME()"

...

Which is what I'm currently using.

@JoaoPaes-at-Dynamox
Copy link
Author

Just figured out that you included the parameter within the 3.4 release. I'm closing this.

@tswast tswast added the type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design. label Jan 22, 2024
@tswast
Copy link
Contributor

tswast commented Jan 22, 2024

Closed by #1408

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 googleapis/python-bigquery API. type: feature request ‘Nice-to-have’ improvement, new feature or different behavior or design.
Projects
None yet
Development

No branches or pull requests

3 participants