<p dir="auto"><strong>Migrated issue, originally created by Konsta Vesterinen (<a href="https://github.com/kvesteri">@kvesteri</a>)</strong></p>
<p dir="auto">The following code illustrates this problem:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base


engine = sa.create_engine(
    'postgres://postgres@localhost/test'
)
engine.echo = True
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()


class Question(Base):
    __tablename__ = 'question'
    id = sa.Column(sa.Integer, primary_key=True)

    weight = sa.Column(sa.Integer, default=1)


Base.metadata.create_all(bind=session.bind)


q = Question(weight=None)
session.add(q)
session.commit()

assert q.weight is None"><pre class="notranslate"><code class="notranslate">import sqlalchemy as sa
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base


engine = sa.create_engine(
    'postgres://postgres@localhost/test'
)
engine.echo = True
Base = declarative_base()
Session = sessionmaker(bind=engine)
session = Session()


class Question(Base):
    __tablename__ = 'question'
    id = sa.Column(sa.Integer, primary_key=True)

    weight = sa.Column(sa.Integer, default=1)


Base.metadata.create_all(bind=session.bind)


q = Question(weight=None)
session.add(q)
session.commit()

assert q.weight is None
</code></pre></div>
<p dir="auto">The problem also exists by changing the <code class="notranslate">default=1</code> to <code class="notranslate">server_default='1'</code>. Also in this case the explicit weight=NULL is not added to INSERT statement.</p>
<p dir="auto"><strong>Migrated issue, originally created by Oleg Pidsadnyi (<a href="https://github.com/olegp">@olegp</a>)</strong></p>
<p dir="auto">Mike, I've encountered a behavior that is a bit unexpected:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="class Model(Base):
    a = Column(Integer, nullable=True, default=1)

m = Model()
m.a = None
session.add(m)

In: print m.a
Out: None

session.commit()

In: print m.a
Out: 1
"><pre class="notranslate"><code class="notranslate">class Model(Base):
    a = Column(Integer, nullable=True, default=1)

m = Model()
m.a = None
session.add(m)

In: print m.a
Out: None

session.commit()

In: print m.a
Out: 1

</code></pre></div>
<p dir="auto">None is expected :(</p>
<p dir="auto">I understand that the configuration is silly, but this is the existing code. Setting None to the column doesn't cancel the default and the default thinks that the column wasn't set, so it uses the default value in the end.</p>