【问题标题】:Rails, PostgreSQL, Heroku - how to change the ":null => false" rule?Rails、PostgreSQL、Heroku - 如何更改“:null => false”规则?
【发布时间】:2012-04-09 10:45:09
【问题描述】:

我正在 localhost 上开发我的应用程序(Rails 3.2、PostgreSQL 9.1),在那里一切正常,所以我决定将应用程序部署到 Heroku 的生产环境中。

成功部署应用后,我尝试创建新项目并收到以下错误消息:

ActiveRecord::StatementInvalid (PG::Error: ERROR:  null value in column "persistence_token" violates not-null constraint

关于桌子Users(使用Authlogic gem)。这是迁移示例 - 关键部分:

class CreateUsers < ActiveRecord::Migration
  def self.up
     create_table :users do |t|
       t.string    :name,                :null => false   
       t.string    :email,               :null => false                                           
       t.string    :crypted_password,    :default => nil, :null => true             
       t.string    :password_salt,       :default => nil, :null => true          
       t.string    :email,               :default => nil, :null => true          
       t.string    :persistence_token,   :null => false      
       t.string    :single_access_token, :null => false              
       t.string    :perishable_token,    :null => false            
       t.datetime  :last_request_at                                    
       t.datetime  :current_login_at                                   
       t.datetime  :last_login_at                                      
       t.string    :current_login_ip                                   
       t.string    :last_login_ip                                      

       t.timestamps
     end
   end
end

什么可能导致这个问题?我的意思是,我在迁移中看到的是规则 :null =&gt; false 并且错误消息说明了保存到此列中的空值,但是如何在 localhost 上很好地工作而在 Heroku 上却不行?

而且,我该如何解决这个问题?我的第一个想法是创建一个新的迁移,在那里我为该列设置:null =&gt; true,但它是否正确?

【问题讨论】:

  • 设置:null =&gt; true 只允许列中的null 值,隐藏原始问题并打开一罐蠕虫。

标签: validation heroku authlogic ruby-on-rails-3.2 postgresql-9.1


【解决方案1】:

我将默认值设置为DEFAULT '',错误就消失了……

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-10
    • 2014-07-19
    • 2012-04-18
    • 1970-01-01
    • 2011-12-30
    • 1970-01-01
    • 1970-01-01
    • 2013-10-25
    相关资源
    最近更新 更多