I've a Table
with the following column:
Column('type', String(128))
How do I set this column to NULL when inserting a new row in database?
I tried doing this:
self.type = NULL;
There is no NULL
type in Python so I don't know how to do it.
From SQLAlchemy docs: nullable – If set to the default of True, indicates the column will be rendered as allowing NULL, else it's rendered as NOT NULL. This parameter is only used when issuing CREATE TABLE statements.
Unlike other programming languages such as PHP or Java or C, Python does not have a null value. Instead, there is the 'None' keyword that you can use to define a null value.
NULL is a special character when inserting into sql (without quotes). If you use the second insert, it would insert the string "NULL", bu the first will have a statement like: INSERT INTO table (var) VALUES (NULL) which will put a special NULL in for the value.
Columns are nullable by default The default value of SQLAlchemy nullable is False unless it's a primary key. A foreign key is also nullable by default.
Instead of trying
self.type = NULL
try as Yaroslav Admin suggested
self.type = None
As Python's equivalent for NULL is None.
I know this is an old thread but this worked for me
self.type = sqlalchemy.sql.null()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With