0

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
1
  • Please post the code of your model AccountLog Commented Aug 2, 2011 at 16:03

2 Answers 2

1

type appears to be a reserved, although deprecated, keyword in ruby.

You probably want to rename your attribute to something else.

Sign up to request clarification or add additional context in comments.

1 Comment

class AccountLog < ActiveRecord::Base belongs_to :user end
0

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.

List of Reserved Words

You can see that 'type' is at the bottom, in the 'other words that have caused trouble'.

3 Comments

ardavis, not sure I got that one. What do you mean?
That was it. When I changed the column name it all works. Quite missleading error though. Should't the interpetor have caught that one long before SQL strings where created?
I'm afraid I don't have an answer to that. But please make sure you mark the correct answer as the answer, and please vote up any helpful responses. Glad you got it working!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.