2015-10-17 52 views
0

從SQLAlchemy的文檔:如何`可空= FALSE`工作SQLAlchemy的

可爲空 - 如果設置爲真默認,表示該列將 呈現爲允許NULL,否則它呈現爲NOT NULL 。這個 參數僅在發出CREATE TABLE語句時使用。

我認爲設置nullable=True爲一個列基本上使該列需要。例如:

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> 

這是怎麼回事?

+1

http://stackoverflow.com/questions/20718469/validation-in-sqlalchemy#comment31059318_20718469 –

回答

1

當你第一次創建一個Location實例並將它提交給數據庫時,'可空'設置爲True?如果是這樣,sqlalchemy將不允許您隨後將可空參數重置爲'False'。您將不得不使用Alembic遷移您的數據庫