Closed zmoshansky closed 9 years ago
I cannot reproduce this in our test suite. Sorry to ask but are you sure your user model was saved and compiled?
No problem at all. Yes, I've double checked to make sure the user
instance had been saved by re-loading it from the database. I also ran mix clean
before running my test again.
I've included fuller code below, un-commenting the lines fixes the problem. The user is generated by a Fixture that uses Blacksmith
. However, since I'm the reloading the model from the DB I wouldn't expect any problems from user.
def save_one(repo, map) do
IO.inspect map
user = Fixture.User.saved_user(MyApp.Repo)
user = repo.get!(MyApp.User, user.id)
address = Ecto.Model.build(user, :address, Map.from_struct(map))
# meta = %{ address.__meta__ | source: {nil, "users_addresses"}}
# address = %{address | __meta__: meta}
repo.insert! address
end
The IO.inspect yields:
%MyApp.Address{__meta__: #Ecto.Schema.Metadata<:built>, assoc_id: nil,
city: "Allyton", country: "Antigua and Barbuda", id: nil, inserted_at: nil,
postal_code: "89470-8639", region: "Wyoming",
street_address: "9980 Donald Extensions", suite: "4917", updated_at: nil}
OH! This line:
address = Ecto.Model.build(user, :address, Map.from_struct(map))
Is passing the __meta__
field which overrides the one we are building! :D If you delete the __meta__
key after you call from_struct
you should be good to go. I will change Ecto to do the same.
Map.delete(Map.from_struct(map), :__meta__)
Worked like a charm. Thanks @josevalim!
Reopening so I don't forget to fix this too. :)
@josevalim went ahead and created a PR for this :)
__meta__.source
doesn't seem to be set correctly when building a polymorphic association. This follows from my stackoverflow question. I wouldexpect Ecto.Model.build/3
to set the appropriate source.Stacktrace
Offending Code
user
is a Ecto model as per Models section below;address
looks like:# %{__meta__: #Ecto.Schema.Metadata<:built>, assoc_id: nil, city: "Mariamside", country: "Bouvet Island (Bouvetoya)", id: nil, inserted_at: nil, postal_code: "55155-8667", region: "New York", street_address: "07228 Dayton Tunnel", suite: "04", updated_at: nil}
This line is used to insert into db:
Inspecting it:
Workaround
Models
Mix Versions
phoenix: v1.0.3 phoenix_ecto: v1.2.0 postgrex: v0.9.1 ecto: 1.0.4