【发布时间】:2016-01-16 11:32:04
【问题描述】:
来自 SQLAlchemy 文档:
nullable – 如果设置为默认值 True,则表示该列将是 呈现为允许 NULL,否则呈现为 NOT NULL。这 参数仅在发出 CREATE TABLE 语句时使用。
我认为为 Column 设置 nullable=True 基本上使该 Column 成为必需的。例如:
class Location(db.Model):
__tablename__ = 'locations'
id = db.Column(db.Integer, primary_key=True)
latitude = db.Column(db.String(50), nullable=False)
...
但是,当我创建一个没有纬度字段的 Location 实例时,我没有收到错误消息!
Python 2.7.8 (default, Oct 19 2014, 16:02:00)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> Location(latitude=None)
<app.models.Location object at 0x10dce9dd0>
发生了什么事?
【问题讨论】:
标签: python sqlalchemy flask-sqlalchemy