Rails activeadmin 无法检测到现有的自动增量字段 table

Rails activeadmin cannot detect auto increment field existing table

我现有的 rails 应用程序有一些 table 有一些数据。在使用activeadmin之前,我直接从postgresql客户端进行了CRUD操作。

我不知道是我错过了文档还是这是一个错误:activeadmin 无法检测到我现有的 table.

的自动增量 ID

如果我刷新提交的表单直到自动递增 ID 超过我 table 中的现有 ID,它就可以工作。

首先我想到的是你在permit params中传递了id参数。 请检查,如果存在则将其删除。

其次,如post中所述,数据库中已有数据,因此生成的序列存在问题,因为它们只能使用一次。

解决方案是使用如下查询将 song_artists.id 列的序列设置为 table 中的最高值:

SELECT setval('song_artist_id_seq', (SELECT max(id) FROM song_artists));

我假设您的序列名称 "song_artist_id_seq"、table 名称 "song_artist" 和列名称 "id"。

要获取序列名称 运行 下面提到的命令:

SELECT pg_get_serial_sequence('tablename', 'columname'); 

从 rails 控制台重置 postgres 序列:

ActiveRecord::Base.connection.tables.each do |t|
 ActiveRecord::Base.connection.reset_pk_sequence!(t)
end

另一种解决方案是覆盖 song_artist class 中的 save() 方法以手动设置新记录的 song_artist id,但不建议这样做。