SQLAlchemy set default nullable=False
Question:
I’m using SQLAlchemy for Flask to create some models. The problem is, nearly all my columns need nullable=False
, so I’m looking for a way to set this option as default when creating a column. Surely I could add them manually (as a Vim exercise), but I just don’t feel like it today. For a reference, this is how my setup (models.py
) looks like:
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), nullable=False)
and many more. Is there a simple way of doing this?
Thanks in advance.
Answers:
just create a wrapper that sets it
def NullColumn(*args,**kwargs):
kwargs["nullable"] = kwargs.get("nullable",True)
return db.Column(*args,**kwargs)
...
username = NullColumn(db.String(80))
using functools.partial
as recommended in the comments
from functools import partial
NullColumn = partial(Column,nullable=True)
As Column
is a class, subclassing might be a lot more extensible depending on your use case.
from sqlalchemy import Column as Col
class Column(Col):
def __init__(self, *args, **kwargs):
kwargs.setdefault('nullable', False)
super().__init__(*args, **kwargs)
The accepted answer creates a wrapper for a nullable = True
column called NullColumn
, which is already what Column
does by default. The question asks to set nullable = False
(not True
) as the default when creating a column.
from sqlalchemy import Column as Col
def Column(*args, **kwargs):
kwargs.setdefault('nullable', False)
return Col(*args, **kwargs)
This function returns a Column
with nullable = False
by default, which can also be overwritten with nullable = True
if needed (unlike the functools.partial
solution).
I’m using SQLAlchemy for Flask to create some models. The problem is, nearly all my columns need nullable=False
, so I’m looking for a way to set this option as default when creating a column. Surely I could add them manually (as a Vim exercise), but I just don’t feel like it today. For a reference, this is how my setup (models.py
) looks like:
from flask.ext.sqlalchemy import SQLAlchemy
db = SQLAlchemy()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), nullable=False)
and many more. Is there a simple way of doing this?
Thanks in advance.
just create a wrapper that sets it
def NullColumn(*args,**kwargs):
kwargs["nullable"] = kwargs.get("nullable",True)
return db.Column(*args,**kwargs)
...
username = NullColumn(db.String(80))
using functools.partial
as recommended in the comments
from functools import partial
NullColumn = partial(Column,nullable=True)
As Column
is a class, subclassing might be a lot more extensible depending on your use case.
from sqlalchemy import Column as Col
class Column(Col):
def __init__(self, *args, **kwargs):
kwargs.setdefault('nullable', False)
super().__init__(*args, **kwargs)
The accepted answer creates a wrapper for a nullable = True
column called NullColumn
, which is already what Column
does by default. The question asks to set nullable = False
(not True
) as the default when creating a column.
from sqlalchemy import Column as Col
def Column(*args, **kwargs):
kwargs.setdefault('nullable', False)
return Col(*args, **kwargs)
This function returns a Column
with nullable = False
by default, which can also be overwritten with nullable = True
if needed (unlike the functools.partial
solution).