0

I have the below pydantic model with 6 columns out of which 2 columns are nullable.

from pydantic import BaseModel
from typing import Optional

class Purchases(BaseModel):
    customer_id: int
    customer_name: str
    purchase_date: str
    city: str
    customer_nickname: Optional[str] = None
    customer_address_type: Optional[str] = None
 
    def insert_purchases(prchs: Purchases) -> None:
        insert_qry = f"""
        INSERT INTO cust_db.purchases(customer_id, customer_name, purchase_date, city, customer_nickname, customer_address_type)
        VALUES ({prchs['customer_id']}, 
        '{prchs['customer_name']}', 
        '{prchs['purchase_date']}', 
        '{prchs['city']}',
        '{prchs['customer_nickname']}',
        '{prchs['customer_address_type']}'
        )"""
        print(insert_qry)
        spark.sql(insert_qry)

My input is as follows:

purchase_input = {"customer_id": 3, "customer_name": "John Doe", "purchase_date": "2011-12-27 13:04:52", "city": "New York", "customer_nickname": None, "customer_address_type": None}

I performed the INSERT operation using the below lines:

prch_obj = Purchases
prch_obj.insert_purchases(purchase_input)

But both the columns customer_nickname and customer_address_type are getting a string type 'None' instead of null values:

INSERT INTO cust_db.purchases(customer_id, customer_name, purchase_date, city, customer_nickname, customer_address_type)
        VALUES (3, 
        'John Doe', 
        '2011-12-27 13:04:52', 
        'New York',
        'None',
        'None'
        )

enter image description here

The only way I'm able to load null values into these fields is by using CASE in my INSERT statement for those two fields:

def insert_purchases(prchs: Purchases) -> None:
    insert_qry = f"""
    INSERT INTO cust_db.purchases(customer_id, customer_name, purchase_date, city, customer_nickname, customer_address_type)
    VALUES ({prchs['customer_id']}, 
    '{prchs['customer_name']}', 
    '{prchs['purchase_date']}', 
    '{prchs['city']}',
    CASE WHEN '{prchs['customer_nickname']}' = 'None' THEN NULL else '{prchs['customer_nickname']}' END,
    CASE WHEN '{prchs['customer_address_type']}' = 'None' THEN NULL else '{prchs['customer_address_type']}' END
    )"""
    print(insert_qry)
    spark.sql(insert_qry)

which then correctly inserts null values:

enter image description here

But I do have many tables where I need handle the incoming None/NULL type values and I think using CASE is an overkill when I have to do it on hundreds of fields.

Is there a better way to achieve this?

1 Answer 1

0

When you use an f string you are converting everything to strings. Since str(None) == 'None' you will simply end up the string 'None'. There are more ways two handle this. (The best would in my opinion be some sort of orm like sqlalchemy to properly handle the data types). However if you just want a quick fix in python:

insert_qry = f"""
        INSERT INTO cust_db.purchases(customer_id, customer_name, purchase_date, city, customer_nickname, customer_address_type)
        VALUES ({purchase_input["customer_id"]}, 
        '{purchase_input["customer_name"]}', 
        '{purchase_input["purchase_date"]}', 
        '{purchase_input["city"]}',
        {f"'{purchase_input["customer_nickname"]}'" if purchase_input["customer_nickname"] else "NULL"},
        {f"'{purchase_input["customer_address_type"]}'" if purchase_input["customer_address_type"] else "NULL"},
        )"""

NOTE: In no means is this clean python code but it would work.

Sign up to request clarification or add additional context in comments.

2 Comments

I get SyntaxError: f-string: f-string: unmatched '[' when I try this.
You maybe have to take the nested f string outside and set the variables first. It also might handle the f string different depending on the python version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.