I have the following rails migration:
我有以下rails迁移:
create_table :articles do |t|
t.integer :user_id, :allow_null => false
t.integer :genre_id, :allow_null => false
t.string :url, :limit => 255, :allow_null => false
t.string :title, :limit => 60, :allow_null => false
t.text :summary, :limit => 350, :allow_null => false
t.integer :votes_count, :default => 0
t.datetime :published_at, :default => nil
t.timestamps
end
All the fields that are "NOT NULL" are validated in the model first, so I'm wondering if I need to bother having allow_null in the migration? I'm not sure what benefits "NOT NULL" gives to the database, if any.
首先在模型中验证所有“NOT NULL”字段,所以我想知道我是否需要在迁移中烦扰allow_null?我不确定“NOT NULL”给数据库带来了什么好处,如果有的话。
3 个解决方案
#1
11
Not much if you mean in terms of performance or storage efficiency. However, it's just good practice to push as many of your low-level constraints into the database layer. For one thing, it guarantees that a subtle bug in Rails isn't going to lead to some randomly NULL
data in a not-null field. Likewise, if you ever run another app against the same database, it will be extremely helpful to have the constraints in a central place for maintenance and to avoid duplication.
如果您的性能或存储效率方面的意思不大。但是,将尽可能多的低级约束推送到数据库层是一种很好的做法。首先,它保证Rails中的细微错误不会导致非空字段中的一些随机NULL数据。同样,如果您针对同一个数据库运行另一个应用程序,那么将约束放在一个中心位置进行维护并避免重复将非常有帮助。
#2
3
NOT NULL is one more thing where the computer can keep an eye on you and keep you from making mistakes.
NOT NULL是计算机可以密切关注您并防止您犯错误的另一件事。
#3
1
This doesn't seemt o affect mySQL, but you should be aware of 2 recently fixed migration issues (one when you don't specify default)
这似乎不会影响mySQL,但你应该知道2个最近修复的迁移问题(当你没有指定默认值时)
http://antoniocangiano.com/2008/07/14/a-close-look-at-three-rails-21-bugs/
#1
11
Not much if you mean in terms of performance or storage efficiency. However, it's just good practice to push as many of your low-level constraints into the database layer. For one thing, it guarantees that a subtle bug in Rails isn't going to lead to some randomly NULL
data in a not-null field. Likewise, if you ever run another app against the same database, it will be extremely helpful to have the constraints in a central place for maintenance and to avoid duplication.
如果您的性能或存储效率方面的意思不大。但是,将尽可能多的低级约束推送到数据库层是一种很好的做法。首先,它保证Rails中的细微错误不会导致非空字段中的一些随机NULL数据。同样,如果您针对同一个数据库运行另一个应用程序,那么将约束放在一个中心位置进行维护并避免重复将非常有帮助。
#2
3
NOT NULL is one more thing where the computer can keep an eye on you and keep you from making mistakes.
NOT NULL是计算机可以密切关注您并防止您犯错误的另一件事。
#3
1
This doesn't seemt o affect mySQL, but you should be aware of 2 recently fixed migration issues (one when you don't specify default)
这似乎不会影响mySQL,但你应该知道2个最近修复的迁移问题(当你没有指定默认值时)
http://antoniocangiano.com/2008/07/14/a-close-look-at-three-rails-21-bugs/