ActiveRecord::StatementInvalid: PG::Error: 错误: 列“id”中的空值违反了非空约束

2023-11-22

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

导轨 - 3.1.3

红宝石 - 1.9.3

ActiveRecord::StatementInvalid: PG::Error: 错误: 空值 列“id”违反非空约束

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 。

任何人都可以帮助我吗?我在这个错误中苦苦挣扎

提前致谢,

塞尔瓦车直安


我有一次同样的问题。这是我的问题。我做了一个

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"

我不确定你的问题,这就是我解决我的问题的方法。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ActiveRecord::StatementInvalid: PG::Error: 错误: 列“id”中的空值违反了非空约束 的相关文章

随机推荐

  • 如何将 HiveQL 查询的结果输出到 CSV?

    我们希望将 Hive 查询的结果放入 CSV 文件中 我认为命令应该如下所示 insert overwrite directory home output csv select books from table 当我运行它时 它说它已成功完
  • 如何使用自定义IPasswordHasher?

    我实现了 IPasswordHasher public class MyPasswordHasher IPasswordHasher public string HashPassword string password using SHA2
  • Android Studio 像素化预览

    将 Android studio 更新到版本 3 1 2 后 出现预览图像像素化 模糊的问题 谁能帮我解决这个问题吗 这就是图像的渲染方式 我在 Android Studio 3 3 中遇到了同样的问题 但幸运的是我已经找到了解决方案 您必
  • C# 中两个列表相交

    我有两个清单 List
  • 是如何实现的?

    C 没有 据我所知 重载或模板 对吧 那么 在普通的 C 语言中如何存在一组与类型无关的同名函数呢 通常的编译时技巧会涉及一大堆宏 不是吗 关于它在 GCC 中如何工作有一个很好的解释here 另外 如果有人能解决中等难度的练习 我很想知道
  • 更快的排列生成器

    我为 Scala 列表编写了一个排列生成器 它生成给定列表的所有排列 到目前为止 我已经得到以下基于这个 Haskell 实现 我认为它比我尝试过的其他几个选项更有效 有什么方法可以提高效率 或者我已经涵盖了所有基础吗 For each e
  • 通过Curl/PHP查询API

    我正在查看 Parse com REST API 并使用 PHP 使用的 Curl 包装器进行调用 原始 Curl 代码 有效 curl X GET H X Parse Application Id myApplicationID H X
  • Struts2 文件上传最大大小

    我需要上传最多 10 MB 的文件 我使用了以下内容 在我的 struts xml 中 我配置如下
  • 让 VB6 显示哪个组件没有安装设计时许可证

    我继承了一个我正在尝试 制作 的 VB6 项目 构建在 制作 EXE 步骤中失败 并出现许可错误 License information for this component not found You do not have an app
  • C# 代码在后台以静默模式运行我的 installer.exe 文件,[重复]

    这个问题在这里已经有答案了 我有这个 C 代码 string desktopPath Environment GetFolderPath Environment SpecialFolder Desktop ProcessStartInfo
  • mysql中的同步存储过程执行

    我有一个存储过程mysql也就是说 执行需要同步的任务 这样 如果两个应用程序调用存储过程 则只有一个应用程序可以访问一段代码来执行该任务 而另一个应用程序将被阻止 直到第一个应用程序完成任务 DELIMITER CREATE PROCED
  • Visual C++:#include 来自同一解决方案中其他项目的文件

    我正在使用 Visual C 开发游戏 我在单独的项目中有一些组件 并设置了项目依赖项 如何 include 来自不同项目的头文件 我不知道如何在另一个项目中使用一个项目中的类 编译器的设置 在您想要 include 头文件的项目中anot
  • 如何在 Scala 中使用正则表达式进行模式匹配?

    我希望能够找到单词的第一个字母与组中的一个字母 例如 ABC 之间的匹配项 在伪代码中 这可能看起来像 case Process word gt word firstLetter match case a c A C gt case gt
  • 在 R markdown 中使用内联带空格的变量名称

    我怎样才能包含内联R引用包含空格或其他异常字符的变量名的代码 实际用例是Pr gt F 反引号是解决方案简单地R脚本 但当代码内联在 Markdown 文档中时它们似乎不起作用 这是一个例子 r df lt data frame mydat
  • 如何针对特定查询优化表?

    您使用哪些模式来确定频繁查询 如何选择优化因素 人们可以做出哪些类型的改变 这是一个很好的问题 虽然相当广泛 但也并不更糟 如果我理解你的意思 那么你是在问如何从头开始解决优化问题 首先要问的问题是 是否存在性能问题 如果没有问题 那么就完
  • Lambda 表达式和高阶函数

    如何使用带有闭包的 Java 8 编写支持将函数作为参数并返回函数作为值的方法 在 Java Lambda API 中 主类是java util function Function 您可以像使用所有其他引用一样使用对此接口的引用 将其创建为
  • Powershell 4 Get-ScheduledTask 和 Windows

    我认为无论您使用什么操作系统 如果安装了 Powershell 您都可以访问相同的默认 cmdlet 所以我想用获取计划任务在我的 Windows 7 机器上 我安装了 Powershell 4 但是 当我运行它时 我收到错误 Get Sc
  • iOS:自动布局导致 UIScrollView 不滚动

    我已经设置了一个UIScrollView我想用它水平显示 12 张图像 屏幕上只能显示 8 张 在下图中 您可以看到我遇到的问题 这使得我的滚动视图无法滚动 我的约束和UIScrollView我已将其添加到故事板上 我已经调用了以下方法 v
  • Play 框架 CORS 标头

    我正在尝试为我的 Play 框架应用程序设置 CORS 标头 具体来说我收到这个错误 cannot load http 127 0 0 1 9000 No Access Control Allow Origin header is pres
  • ActiveRecord::StatementInvalid: PG::Error: 错误: 列“id”中的空值违反了非空约束

    我在我的应用程序中经常收到以下错误 如果我重新启动 Rails 应用程序 此错误将在一段时间内不会出现 导轨 3 1 3 红宝石 1 9 3 ActiveRecord StatementInvalid PG Error 错误 空值 列 id