Autogenerated UUID not returned after insert

I have a model with bigint primary key and uuid field with UUID column type for external use. I am autogenerating it with with default: fragment("uuid_generate_v4()") in migration. The thing is when I insert anything this uuid field is autogenerated but not returned. I have to implicitly query again to get it.

How do I make it return after insert query? Is there better way to put uuid while inserting without using callbacks?

There’s a read_after_write option for fields in your schema definition.


Thank you, that solved it

Another alternative is to add @primary_key {:id, :binary_id, autogenerate: true} above your schema than Ecto will generate UUID’s for you so they will be accessible without using read_after_write (which isn’t supported by MySQL if I recall correctly).

Yeah, thats direct approach but I am not using UUID as primary key. They are only used for outside apis. So read_after_write worked fine.

1 Like