Ecto cast Embed with key

I am trying to add json to postgres.

I am sending json from postman

{ "Name":"name", "address": [{ "address1": "address1", "address2": "11", "address3": "", "city": "city", "town": "", "state": "", "zipcode": "458770" }] }

Then using cast_embed to add to database

In my user schema
embeds_many :address, EmbedAddress, on_replace: :delete

Then

|> cast_embed(:address, with: &EmbedAddress.changeset/2)

And when inserting its adding list to field. Like

[{“address1”:“address1”,“address2”:null,“address3”:null,“city”:“city”,“country”:“country”,“desc”:null,“inserted_at”:“2019-11-17T21:31:52”,“party_id”:null,“state”:“hidd”,“town”:“hidd”,“type”:“P”,“updated_at”:“2019-11-17T21:31:52”,“zipcode”:“458770”}]

I want to add list with key like :
{“address”: [{“address1”:“address1”,“address2”:null,“address3”:null,“city”:“city”,“country”:“country”,“desc”:null,“inserted_at”:“2019-11-17T21:31:52”,“party_id”:null,“state”:“hidd”,“town”:“hidd”,“type”:“P”,“updated_at”:“2019-11-17T21:31:52”,“zipcode”:"458770”}]}

How can I keep address key in changeset or can add address key.

Thanks for help

can any body please help…

Can you clarify why you’d want the column name within the column data itself? Is it possible you’re confusing the column data with the model schema?

Your user schema embeds many addresses in the :address field, and is therefore modeled as an array of (json) addresses (which is what you’re reporting in “when inserting its adding list to field”). This is the expected behavior. Later, when you query a user from the DB, you’ll be able to access my_user.address and get the array of addresses.

If you want to have an address key in the column data itself, then your user schema no longer embeds_many (since it’s instead embedding a single json object instead of an array of objects) and it needs to be changed, e.g.:

embeds_one :address, EmbeddedAddressContainer, on_replace: :delete

where EmbeddedAddressContainer in turn embeds_many :address, EmbedAddress, on_replace: :delete.

But this modeling seems a bit peculiar, so if you explain your use case around needing to have the address key stored in the column itself, perhaps you could get better help.

i am trying to achive update on jsonb

https://www.idalko.com/crud-operations-poffstgres-jsonb/

it seems like its very easy if there is a key

UPDATE USER_CONFIGURATIONS SET DATA = JSONB_SET( DATA, '{configurations}'::text[], (((DATA -> 'configurations') - (SELECT i FROM generate_series(0, jsonb_array_length(data->'configurations') - 1) AS i WHERE (data->'configurations'->i->>'name' = 'firstconf')))::jsonb || '{"name":"first", "theme":"light"}'::jsonb)) WHERE (DATA ->> 'userid') = 'artur@gmail.com';

if you can help me with update jsonb opreation that will be very helpful

As far as I can tell, what you currently have in your schema should work: the users.address column will contain an array of json data which you can then query with postgres’ syntax.