Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails:PG::NotNullViolation:ERROR:null值位于列";id";违反非空约束_Ruby On Rails_Postgresql - Fatal编程技术网

Ruby on rails Rails:PG::NotNullViolation:ERROR:null值位于列";id";违反非空约束

Ruby on rails Rails:PG::NotNullViolation:ERROR:null值位于列";id";违反非空约束,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,以下是将记录保存到rails 3.2.12和9.3页上的postgres数据库时出现的错误: ActiveRecord::StatementInvalid (PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint : INSERT INTO "sw_module_infox_module_infos" ("about_controller", "about_init", "about

以下是将记录保存到rails 3.2.12和9.3页上的postgres数据库时出现的错误:

ActiveRecord::StatementInvalid (PG::NotNullViolation: ERROR:  null value in column "id" violates not-null constraint
: INSERT INTO "sw_module_infox_module_infos" ("about_controller", "about_init", "about_log", "about_misc_def", "about_model", "about_onboard_data", "about_subaction", "about_view", "about_workflow", "active", "api_spec", "category_id", "created_at", "last_updated_by_id", "module_desp", "name", "submit_date", "submitted_by_id", "updated_at", "version", "wf_state") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21) RETURNING "id"):
  activerecord (3.2.12) lib/active_record/connection_adapters/postgresql_adapter.rb:1166:in `get_last_result'
  activerecord (3.2.12) lib/active_record/connection_adapters/postgresql_adapter.rb:1166:in `exec_cache'
到目前为止,该表工作正常(在今天发生错误之前保存了大约50条记录)。在pgadmin中打开pg表后。我们发现表上的
id
integer
。我们还发现另一张表上的
Id
serial
。似乎id应该是
串行的
,这样它可以自动递增。如果是,那么如何将id列从
整数
转换为
串行
?如果不是,那么如何解决这个问题

数据类型不是真正的类型,只是创建唯一标识符列的一种符号方便(类似于一些其他数据库支持的
AUTO_INCREMENT
属性)

如果列是
整数
,则无法将其转换为
序列
,但可以模拟PostgreSQL的操作,就像使用序列创建表一样:

CREATE SEQUENCE tablename_colname_seq; ALTER TABLE tablename ALTER COLUMN colname SET DEFAULT nextval('tablename_colname_seq'::regclass); ALTER SEQUENCE tablename_colname_seq OWNED BY tablename.colname; 创建序列tablename\u colname\u seq; ALTER TABLE tablename ALTER COLUMN colname SET DEFAULT nextval('tablename\u colname\u seq'::regclass);
更改tablename.colname拥有的序列tablename\u colname\u seq 我在使用Rails 6应用程序时遇到了这样的问题

我有一个用户模型,用于创建
学生
管理员

class User < ApplicationRecord
  belongs_to :role
end
学生模型:

class Role < ApplicationRecord
end
class Admin < ApplicationRecord
  before_save :set_admin_role

  belongs_to :user

  private

  def set_admin_role
    # set role_id to '1' except if role_id is not empty
    user.role_id = '1' if user.role_id.nil?
  end
end
class Student < ApplicationRecord
  before_save :set_student_role

  belongs_to :user

  private

  def set_student_role
    # set role_id to '2' except if role_id is not empty
    user.role_id = '2' if user.role_id.nil?
  end
end
以下是我解决问题的方法

问题是
users
表中的
role\u id
在我的迁移文件中被设置为
null:false
。我不得不将其更改为
null:true

class CreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      t.string :email
      t.string :password_digest
      t.references :role, null: true, foreign_key: true

      t.timestamps
    end
  end
end
就这些


我希望这有帮助

我也有类似的问题

我会告诉你是怎么回事,也许有人会发现我的经验很有用

我有
错误:列“created_at”中的空值违反了非空约束

问题是,我有一个方法,在创建之前运行
,在保存之前运行
,这个方法试图保存一些小的更新。因此,它试图节省几次

我的建议是,在保存之前,检查您的代码在
中执行的操作
和创建前的


“serial”只是
整数的别名,带有
默认的nextval('some_sequence')
;请参阅用户手册。表是使用rails rake db:migrate创建的。33个表中大约有8个是整数类型。其余的是连续的。有些表也缺少索引。不确定是什么导致了postgres的问题。sqlite创建的同一个表根本没有这样的问题。是什么导致了这个问题?sqlite也使用了相同的迁移,从来没有出现过这样的问题。33个表中约有8个表的id为整数(非串行)。此外,其中一些也缺少索引。它开始困扰我们,因为我们不知道为什么它会发生在博士后身上。我们认为postgres比sqlite更健壮。@user938363同样,serial不是真正的类型。您的所有列都是
integer
(在PgAdmin中,您可以在其属性>数据类型中检查它)。列之间的差异只是默认值&它们序列的存在。-这些表在ruby中的定义肯定有一些不同,所以rails决定不将它们像序列一样构建,但这应该是另一个问题(主要与rails/rake相关)@@pozs,我们需要将序列设置为下一个可用id吗?或者上面的3个命令已经做到了这一点。@user938363这只会修复结构,如果您的值也有问题,请看这是完整的文档,并且正是我要找的!谢谢你为我节省了时间!
class CreateUsers < ActiveRecord::Migration[6.0]
  def change
    create_table :users do |t|
      t.string :email
      t.string :password_digest
      t.references :role, null: true, foreign_key: true

      t.timestamps
    end
  end
end
class User < ApplicationRecord
  belongs_to :role, optional: true
end