Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask SqlAlchemy: Returning None for JSON type inserts 'null' string, not real NULL

I'm using Flask SqlAlchemy. I'm inserting data into a Postgres DB and there is a JSON column type. When there is no data, I return None with the intention that that will set NULL into the column. This is what's suggested here.

if someCondition:
    json_obj = json.dumps(my_dict)
    return json_obj
else:
    return None # this inserts a 'null' string incorrectly
    # Only this works correctly:
    return null() # imported from sqlalchemy, 

But I see that this inserts a "null" string. The only way to get a real NULL is to use sqlalchemy.null(). Someone else noticed the same thing in this thread. Below is the difference from the two approaches. Can someone confirm that None should not be used for NULL insertion, to clear up the confusion?

enter image description here

like image 324
gene b. Avatar asked Apr 28 '26 01:04

gene b.


1 Answers

What you describe is the default behavior.

The JSON and JSONB column type can be initialized with the parameter none_as_null set to True. This parameter is by default False.

The documentation for reference.

like image 112
rfkortekaas Avatar answered Apr 30 '26 17:04

rfkortekaas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!