ydb-platform / ydb-python-sdk

YDB Python SDK
https://ydb-platform.github.io/ydb-python-sdk/
Apache License 2.0
87 stars 50 forks source link

How can I insert null value in some columns #522

Open SergiusAC opened 1 week ago

SergiusAC commented 1 week ago

I need to execute INSERT query where some columns should be null. How can I do this?

My code example:

query = """
DECLARE $user_id AS String;
DECLARE $tg_user_id AS String;
DECLARE $activate_token AS String;
DECLARE $notifications_enabled AS Bool;
DECLARE $created_at AS Datetime;

INSERT INTO tg_users (user_id, tg_user_id, activate_token, notifications_enabled, created_at)
VALUES ($user_id, $tg_user_id, $activate_token, $notifications_enabled, $created_at);
"""
params = {
    "$user_id": (user_id.encode(), ydb.PrimitiveType.String),
    "$tg_user_id": None, # this column should be NULL
    "$activate_token": (activate_token.encode(), ydb.PrimitiveType.String),
    "$notifications_enabled": (notifications_enabled, ydb.PrimitiveType.Bool),
    "$created_at": (int(datetime.now().timestamp()), ydb.PrimitiveType.Datetime),
}
async with await self.tx.execute(query, params):
    pass