Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert NULL value in SQLAlchemy?

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.

like image 746
bodacydo Avatar asked Oct 05 '15 23:10

bodacydo


People also ask

What is nullable SQLAlchemy?

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.

How do you add a value to a NULL in Python?

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.

How can I insert NULL data into mysql database with Python?

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.

Are SQLAlchemy columns nullable by default?

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.


2 Answers

Instead of trying

self.type = NULL 

try as Yaroslav Admin suggested

self.type = None 

As Python's equivalent for NULL is None.

like image 144
Hayk Davtyan Avatar answered Oct 04 '22 08:10

Hayk Davtyan


I know this is an old thread but this worked for me

self.type = sqlalchemy.sql.null() 
like image 36
omeanwell Avatar answered Oct 04 '22 07:10

omeanwell