Reputation: 539
Weird null error.
I have a /lib/task rake script that refuses to set a string value, when I run it rake reports this output/error:
Purchase
Purchase
PGError: ERROR: null value in column "type" violates not-null constraint : 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') RETURNING "id" C:/Ruby192/lib/ruby/gems/1.9.1/gems/activerecord-3.0.7/lib/active_record/connect ion_adapters/abstract_adapter.rb:207:in `rescue in log'
Which is very very strange since I actually set value which clearly is not NULL. How come???
My code is this:
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
Upvotes: 0
Views: 545
Reputation: 9895
You are using 'type' as an attribute. According to the link below, 'type' may be a reserved word in the ruby language, and could cause some issues like the one you're experiencing.
You can see that 'type' is at the bottom, in the 'other words that have caused trouble'.
Upvotes: 0
Reputation: 3229
type
appears to be a reserved, although deprecated, keyword in ruby.
You probably want to rename your attribute to something else.
Upvotes: 1