AE
Ash Elixir•2y ago
axdc

Creating an Identity referencing a key within a :map attribute (postgres-stored jsonb)

I have a resource with these attributes:
attributes do
uuid_primary_key :id

attribute :source, :atom do
constraints one_of: [:source_one, :source_two]
end

attribute :original_json, :map
end
attributes do
uuid_primary_key :id

attribute :source, :atom do
constraints one_of: [:source_one, :source_two]
end

attribute :original_json, :map
end
I'd like to create an identity on the resource that uses a key within the map. Is that possible?
identities do

#something like this?
identity :synthetic_id [:source, :original_json["Key"]]

end
identities do

#something like this?
identity :synthetic_id [:source, :original_json["Key"]]

end
Specifically, I'd like to upsert using this identity. I'm seeing recommendations for Ecto to just do it manually in the database and wondering if there's an Ash Way.
3 Replies
ZachDaniel
ZachDaniel•2y ago
There is not currently a great way to do this with ash_postgres 😦 You can create the unique constraint easily enough using the custom_indexes tools in ash_postgres But upserting on it is not likely going to work. Well, it definitely won’t, but is unlikely to be something we can make work in the near future.
axdc
axdcOP•2y ago
ok, got it. so for reference what I'm doing right now (as a "workaround", but it works the same) is just extracting the values I want to key on from the json before sending it all to ash, and I have those keys as real attributes in the resource with an identity defined on them. Working great. Those specific keys should be stable. I /would/ like, in another area, to /select and filter/ based on the contents of the jsonb, which is why I figured I'd try to jump right in with upserting on it. Is reading, filtering, etc supported? (The data has over five hundred fields in an unstable "schema", that's why I'm using jsonb. I've been informed postgres wouldn't take kindly to that many columns.)
ZachDaniel
ZachDaniel•2y ago
Yep! You can use bracket access in expressions , I.e map[:key]

Did you find this page helpful?