无法获得与 activerecord 类型存储一起使用的唯一性验证
我们在 https://github.com 使用 activerecord-typedstore
gem /byroot/activerecord-typedstore 表示“您可以使用任何 ActiveModel 验证器”。我能够让 validates :name,present: true
工作,但是 validates :name, uniqueness: true
给我错误 PG::UndefinedColumn
代码>(见下文)。有谁知道我做错了什么?
class CreateWorkspaces < ActiveRecord::Migration[7.0]
def change
create_table :workspaces do |t|
t.json :payload, null: false
end
end
end
class Workspace < ApplicationRecord
typed_store :payload, coder: ActiveRecord::TypedStore::IdentityCoder do |s|
s.string :name
end
validates :name, presence: true
validates :name, uniqueness: true
end
% rails c
Loading development environment (Rails 7.0.2.3)
irb(main):001:0> Workspace.create(name: 'Hello')
TRANSACTION (0.2ms) BEGIN
Workspace Exists? (0.9ms) SELECT 1 AS one FROM "workspaces" WHERE "workspaces"."name" = $1 LIMIT $2 [["name", "Hello"], ["LIMIT", 1]]
TRANSACTION (0.2ms) ROLLBACK
/Users/vince/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/activerecord-7.0.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec_params': PG::UndefinedColumn: ERROR: column workspaces.name does not exist (ActiveRecord::StatementInvalid)
LINE 1: SELECT 1 AS one FROM "workspaces" WHERE "workspaces"."name" ...
We're using the activerecord-typedstore
gem at https://github.com/byroot/activerecord-typedstore which says "You can use any ActiveModel validator". I'm able to get validates :name, presence: true
to work but validates :name, uniqueness: true
gives me the error PG::UndefinedColumn
(see below). Does anyone know what I'm doing wrong?
class CreateWorkspaces < ActiveRecord::Migration[7.0]
def change
create_table :workspaces do |t|
t.json :payload, null: false
end
end
end
class Workspace < ApplicationRecord
typed_store :payload, coder: ActiveRecord::TypedStore::IdentityCoder do |s|
s.string :name
end
validates :name, presence: true
validates :name, uniqueness: true
end
% rails c
Loading development environment (Rails 7.0.2.3)
irb(main):001:0> Workspace.create(name: 'Hello')
TRANSACTION (0.2ms) BEGIN
Workspace Exists? (0.9ms) SELECT 1 AS one FROM "workspaces" WHERE "workspaces"."name" = $1 LIMIT $2 [["name", "Hello"], ["LIMIT", 1]]
TRANSACTION (0.2ms) ROLLBACK
/Users/vince/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/activerecord-7.0.2.3/lib/active_record/connection_adapters/postgresql_adapter.rb:768:in `exec_params': PG::UndefinedColumn: ERROR: column workspaces.name does not exist (ActiveRecord::StatementInvalid)
LINE 1: SELECT 1 AS one FROM "workspaces" WHERE "workspaces"."name" ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据评论,我同意这不是上例中使用 json 作为列类型的最佳方式。如果你仍然想这样做,根据@max的评论添加这个文件
models/concerns/hash_uniqueness_validator.rb
然后你可以这样做
I agree based on the comments that this isn't the best use of json as the column type in the example above. If you still want to do this, based on @max's comment add this file
models/concerns/hash_uniqueness_validator.rb
then you can do this