擅长:python、mysql、java
<p>正如其他答案所指出的,显式NULL可以通过传递<code>None</code>插入,或者在SQLAlchemy a<a href="http://docs.sqlalchemy.org/en/latest/core/sqlelement.html#sqlalchemy.sql.expression.null" rel="nofollow noreferrer">^{<cd2>}</a>构造的情况下,作为值插入。实际上,PEP-249“DB-API v2.0”在<a href="https://www.python.org/dev/peps/pep-0249/#type-objects-and-constructors" rel="nofollow noreferrer">"Type Objects and Constructors"</a>中清楚地说明了这一点:</p>
<blockquote>
<p>SQL NULL values are represented by the Python <code>None</code> singleton on input and output.</p>
</blockquote>
<p>作为第三个选项,如果列可以为空,则可以省略它:</p>
<pre><code>t = Table('t', metadata,
Column('a', Integer),
Column('b', Integer))
stmt = t.insert().values(a=1)
engine.execute(stmt)
</code></pre>
<p>将有效地在表<code>t</code>中插入新行<code>(1, NULL)</code>,因为没有为列<code>b</code>提供值。同样的情况也适用于映射类,我想最初的问题实际上是在使用它(因为<code>self</code>):</p>
<pre><code>class T(Base):
__tablename__ = 't'
id = Column(Integer, primary_key=True)
a = Column(Integer)
b = Column(Integer)
session.add(T(a=1))
session.commit()
</code></pre>
<p>再次有效地导致<code>(default, 1, NULL)</code>被插入。</p>