简体   繁体   English

是否有任何理由在Postgres的Rails迁移中使用`default:nil`?

[英]Is there any reason to use `default: nil` in a Rails migration for Postgres?

I see default: nil in the migration file of a Rails project I'm working on. 我看到default: nil在我正在处理的Rails项目的迁移文件中为default: nil In Postgres this results in a NULL value in the db. 在Postgres中,这会导致db中的NULL值。

Is there any reason why someone would use this? 有人会用这个吗?

It seems to me, the same result can be achieved by just skipping setting a default, and the value will be NULL anyway. 在我看来,通过跳过设置默认值可以实现相同的结果,无论如何该值将为NULL

I'm asking myself the same question right now. 我现在问自己同样的问题。 I have two columns on a model that should always be 'nil' to reflect a legacy system decision. 我在模型上有两列,应始终为'nil'以反映遗留系统决策。

... ...

I don't think there is a reason to do this, as you say, it defaults to nil. 我认为没有理由这样做,正如你所说,它默认为零。 In my case, I've decided to use validates_absence_of :field aswell. 就我而言,我决定使用validates_absence_of :field

It may not exactly be an answer but someone might find comfort that someone else is doing it like this. 它可能不完全是一个答案,但有人可能会觉得其他人正在做这样的安慰。 I'd be keen to know the disadvantages of implicitly assuming the field will just always stay nil . 我很想知道隐含地假设该领域将始终保持nil的缺点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM