ActiveRecord::StatementInvalid: PG::Error: ERROR: 列“id"中的空值违反非空约束 [英] ActiveRecord::StatementInvalid: PG::Error: ERROR: null value in column "id" violates not-null constraint

查看:62
本文介绍了ActiveRecord::StatementInvalid: PG::Error: ERROR: 列“id"中的空值违反非空约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中经常出现以下错误.如果我重新启动 Rails 应用程序,此错误在一段时间内不会出现.

I am getting the following error frequently in my application. If I restart my rails application this error will not appear for some time.

Rails - 3.1.3

Rails - 3.1.3

红宝石 - 1.9.3

Ruby - 1.9.3

ActiveRecord::StatementInvalid: PG::Error: ERROR: null value in列id"违反了非空约束

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

Directory.create(:name=>"name",:alias_name=>"name",:site_id=>2,:type=>"Directory")

(0.3ms)  BEGIN
  SQL (0.8ms)  INSERT INTO "databases" ("alias_name", "created_at", "created_by", "id", "name", "site_id", "type", "updated_at", "updated_by") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)  [["alias_name", "name"], ["created_at", Wed, 17 Apr 2013 16:46:25 IST +05:30], ["created_by", nil], ["id", nil], ["name", "name"], ["site_id", 2], ["type", "Directory"], ["updated_at", Wed, 17 Apr 2013 16:46:25 IST +05:30], ["updated_by", nil]]
PG::Error: ERROR:  null value in column "id" violates not-null constraint
: INSERT INTO "databases" ("alias_name", "created_at", "created_by", "id", "name", "site_id", "type", "updated_at", "updated_by") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
   (0.2ms)  ROLLBACK
ActiveRecord::StatementInvalid: PG::Error: ERROR:  null value in column "id" violates not-null constraint
: INSERT INTO "databases" ("alias_name", "created_at", "created_by", "id", "name", "site_id", "type", "updated_at", "updated_by") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1034:in `get_last_result'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:1034:in `exec_cache'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:603:in `block in exec_query'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:244:in `block in log'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activesupport-3.1.3/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract_adapter.rb:239:in `log'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/postgresql_adapter.rb:601:in `exec_query'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:63:in `exec_insert'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:90:in `insert'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/relation.rb:70:in `insert'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/persistence.rb:313:in `create'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/timestamp.rb:51:in `create'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/callbacks.rb:268:in `block in create'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:390:in `_run_create_callbacks'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activesupport-3.1.3/lib/active_support/callbacks.rb:81:in `run_callbacks'
... 7 levels...
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/validations.rb:50:in `save'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/attribute_methods/dirty.rb:22:in `save'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/connection_adapters/abstract/database_statements.rb:192:in `transaction'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:208:in `transaction'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:241:in `block in save'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/activerecord-3.1.3/lib/active_record/transactions.rb:240:in `save'
    from (irb):9
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/railties-3.1.3/lib/rails/commands/console.rb:45:in `start'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/railties-3.1.3/lib/rails/commands/console.rb:8:in `start'
    from /home/user/.rvm/gems/ruby-1.9.3-p194-firstversion@web/gems/railties-3.1.3/lib/rails/commands.rb:40:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'1.9.3p194 :010 >

我不知道为什么 id 在 rails 生成的 SQL 中变为 nil.

I don't know why id is going as nil in the rails generated SQL.

谁能帮帮我!!!我在这个错误中苦苦挣扎

Can anyone help me please !!! I am struggling lot with this error

提前致谢,

塞尔瓦车志安

推荐答案

我曾经遇到过同样的问题.这是我的问题.我做了一个

I have same problem once. Here is my problem. I did a

rake db:schema:dump
rake db:schema:load

表格出现问题如下:

create_table "campaign_line_items_backup", :id => false, :force => true do |t|
  t.integer  "id"
  t.integer  "campaign_id"
  t.integer  "size_id"
  t.text     "name"

应该是

create_table "campaign_line_items_backup", :force => true do |t|
  t.integer  "campaign_id"
  t.integer  "size_id"
  t.text     "name"

我不确定你的问题,我就是这样解决的.

I am not sure about your problem, That's the way I fixed mine.

这篇关于ActiveRecord::StatementInvalid: PG::Error: ERROR: 列“id"中的空值违反非空约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆