Rails Activerecord。创建奇怪的空错误

时间:2011-08-02 16:01:17

标签: ruby-on-rails activerecord null

奇怪的空错误。

我有一个/ lib / task rake脚本拒绝设置字符串值,当我运行它时rake报告此输出/错误:

购买

购买

PGError:错误:“type”列中的空值违反了非空约束 :INSERT INTO“account_logs”(“sum”,“type”,“usertype”,“transaction_id”,“commited”,“user_id”,“created_at”,“updated_at”) VALUES(168.0,NULL,'Public',452921,'t',10146 ,'2011-07-29 09:57:11.514472','2011-08-02 15:33:38.479838')返回“id” C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connect ion_adapters / abstract_adapter.rb:207:在`rescue in log'中

这是非常奇怪的,因为我实际设置的值显然不是NULL。怎么来???

我的代码是:

  m_sum = Float(rand_int(1,500))
  m_type = "Purchase"

  puts m_type

  m_user_info = UserInfo.order("RANDOM()").first
  m_usertype = m_user_info.usertype
  m_transactionid=gid
  m_commited=true
  m_user_id=rand_int(1,User.count)
  m_created_at=rand_time(1.week.ago,Time.now)
  m_updated_at=Time.now

  if m_type.nil?
    puts "What the f"
  end
  puts m_type

  AccountLog.create(
    :sum               => m_sum,
    :type              => m_type,
    :usertype          => m_usertype,
    :game              => m_game,
    :transactionid     => m_transactionid,
    :commited          => m_commited,
    :user_id           => m_user_id,
    :created_at        => m_created_at,
    :updated_at        => m_updated_at
  )

#part of schema.rb
create_table "accountlogs", :force => true do |t|
  t.decimal  "sum",               :precision => 15, :scale => 10, :null => false
  t.string   "type",              :limit => 30, :null => false
  t.string   "usertype",          :limit => 30, :null => false
  t.integer  "transactionid",     :limit => 8,  :null => false
  t.boolean  "commited",                        :null => false
  t.integer  "user_id",           :limit => 8,  :null => false
  t.datetime "created_at"
  t.datetime "updated_at"
end

#Model

class AccountLog < ActiveRecord::Base
  belongs_to :user
end

2 个答案:

答案 0 :(得分:1)

type似乎是ruby中保留但已弃用的关键字。

您可能希望将属性重命名为其他内容。

答案 1 :(得分:0)

您使用'type'作为属性。根据下面的链接,'type'可能是ruby语言中的保留字,可能会导致一些问题,例如您遇到的问题。

List of Reserved Words

您可以看到“类型”位于底部,“其他导致问题的话”。

相关问题