Comments (5)
@geofflane This is a good point. This is actually expected behavior. Since Ecto doesn't have an id until the record is inserted we can't set the id. Since params_for calls build
and not create
, it does not get the id.
We could call insert
when using params_for, but that could cause performance issues and bugs. Since we would insert the record and the association it would require more database inserts, and it would also insert the record. So if you're testing a create endpoint you would already have those records in the database, making it impossible (or very difficult) to test.
I do see your point though. We have code in one of our apps that does something like this
def play_params do
game = create(:game)
posession_team = create(:posession_team)
params_for(:play, game_id: game.id, posession_team: posession_team.id)
end
Maybe it would be useful to have an option for creating associations when using params_for, but I'm not sure how that would look, or if it would be more trouble than it's worth. params_for_with_associations(:user)
?
For now I think it would be best to write your own function like the one above, but I'm open to ideas!
from ex_machina.
@paulcsmith I mainly put this issue in because it was unexpected behavior to me. The create "does the right thing" and builds the graph of objects in the DB, but the build/params only kind of does. (That params_for model is exactly what I came up with too. I found that pain because I added a constraint that caused a bunch of my tests to fail so I had to go add those associations manually in a bunch of places.)
The use case, for me, is testing an API POST to create a new instance of a thing. I wanted a way to easily construct an instance, but also to have dependent relations exist already. There might be a hybrid in there somewhere between create and build/params_for maybe to support that case?
from ex_machina.
@geofflane Sorry I probably should have phrased that better. What I meant by "expected behavior" was that it is not a bug, but I can definitely see how it is unexpected. One thing I would recommend is to add the params function to your factory or in a module that you import in your tests, that way when you need to change how it works you only need to do it in one place.
As for the params_for function, I doubt that we'll change it to insert associations by default because that could cause unintended side effects. I'm not opposed to adding another function like params_with_association_ids_for(:user)
, or full_params_for(:user)
or something like that.
What do you think @jsteiner. Also @derekprior, we had to create a custom function for handling relationships on our project. What do you think of having a function that inserts associations and sets their ids?
from ex_machina.
@paulcsmith No apologies needed, I understood what you meant. :)
from ex_machina.
Thanks @geofflane! I created another issue #105 for implementing this. Feel free to tackle it if you have time :) If not I can get it in a few weeks
from ex_machina.
Related Issues (20)
- def build/2 defines defaults multiple times HOT 2
- Use of `build( ... )` with Ecto 3.4+ leads to many association preload warnings HOT 7
- Compiler warning when using an a project using Elixir 1.11 HOT 3
- Insert with assocs fails constraint HOT 1
- Proposal: Introduce build_lazy/2 HOT 3
- Test-only implementation fails HOT 4
- Cast polymorphic embeds HOT 8
- Inserted data breaks manual test HOT 3
- Allow sequence in attributes passed to `insert_list` HOT 5
- Hundreds of depreciation compiler warnings when running tests HOT 4
- insert/2 doesn't work with compound primary keys HOT 3
- Allow sequences indices to be persisted between runs HOT 2
- sequence starting with 0
- Is this project maintained? HOT 17
- Sequence reset doesn't respect the `start_at` option
- sequence(:example_id, & &1) put chars instead of integer HOT 3
- Stringify `Ecto.Enum` values when using `string_params`/`string_params_with_assocs`
- Option to have `insert_list` leverage Ecto's `insert_all`
- an intermittent error on async false tests HOT 1
- nvim diagnostics error HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ex_machina.