Just use None
, the Python equivalent of SQL NULL
.
Note: not the string "None"
but the Python singleton None (without quotes).
Question
I have this code and I'm trying to update a data row value to Null to test that it can be reselected on the front-end with Selenium Webdriver. It works fine when I run the sql manually against the data base but when I try to parameter substitute Null for an int column I get
Here is the meaningful part of the stack
cursor.execute(query, (REASONS_CREATED[reason_created], order_id))
DataError: invalid input syntax for integer: "Null"
LINE 4: reason_created_id = E'Null'
Here is my code:
def update_reason_created_on_order(order_id, reason_created, environment='test'):
'''
Updates an order's reason created.
'''
sql_directory = find_path_to_sql_directory()
with open(os.path.join(sql_directory, 'update_reason_created_on_order')) as sql:
cursor = setup_database_cursor(environment)
query = sql.read()
try:
cursor.execute(query, (REASONS_CREATED[reason_created], order_id))
except:
raise
Here is my Null entry for my REASONS_CREATED dictionary lookup
REASONS_CREATED = {'NULL': 'Null'}
Here is my sql in an external file
UPDATE
order
SET
reason_created_id = %s
WHERE
order_id = %s;
COMMIT;
I understand that psycopg2 does automatic type conversion and recognizes this reason_created_id is type int. Does anyone know how to set it to null? Also, the column is nullable.
Solution
Just use None
, the Python equivalent of SQL NULL
.
Note: not the string "None"
but the Python singleton None (without quotes).