Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inserting NULL as default in SQLAlchemy?

I have the following column in SQLAlchemy:

name = Column(String(32), nullable=False)

I want that if no value is passed onto an insertion, it should enter a default value of either completely blank or if not possible, then default as NULL.

Should I define the column as :

name = Column(String(32), nullable=False, default=NULL)

Because in python NULL object is actually None

Any suggestions?

like image 425
sshussain270 Avatar asked Mar 22 '18 10:03

sshussain270


1 Answers

If you want an empty string as default, you can:

name = Column(String(32), nullable=False, default='')

However, if you just do:

name = Column(String(32), nullable=True)

then the default value of the column will be a NULL.

Note that a NULL value indicates “unknown value”, and typically should not be confused with an empty string. If your application needs the concept of “unknown value”, then make your column nullable. If not, make it non-nullable and default to empty strings.

like image 61
tzot Avatar answered Nov 13 '22 14:11

tzot