Comments (6)
Is the contact factory just returning an empty struct or did you remove things from it for the issue? Also, the currently released version of ExMachina has a lot of custom code to save associations since Ecto 1.x did not handle those automatically. Ecto 2.0 and ExMachina 1.0.0 work together nicely and should handle associations better. I would recommend trying those out. As soon as Ecto 2.0 is out of beta, we will also launch ExMachina 1.0 which should fix a lot of these issues
from ex_machina.
Thanks for the heads up regarding Ecto 2.0!
The Contact factory is currently returning an empty struct, since the Contact model is mostly a container to relate several other Models together. I had another version of my code where the Contact factory called build(:contact_profile), but that didn't work right either.
from ex_machina.
@mikegillis677 Hmm this does seem odd. I've seen this before, but I am not entirely sure that it is ExMachina. Can you try dropping your test db and running the test again? Also could you try create(:contact_profile)
by itself and see what happens? Could you also provide the models that are being called. Maybe something in the schema will provide a clue :)
from ex_machina.
create(:contact_profile)
by itself doesn't error.
Here's my models:
defmodule Project.Contact do
use Project.Web, :model
schema "contacts" do
belongs_to :person, Project.Person
has_one :contact_profile, Project.ContactProfile
has_many :conversation_participants, through: [:person, :conversation_participant]
has_many :channel_identifiers, Project.ChannelIdentifier
timestamps
end
@required_fields ~w()
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
end
defmodule Project.ContactProfile do
use Project.Web, :model
schema "contact_profiles" do
field :first_name, :string
field :last_name, :string
field :city, :string
field :region, :string
field :postal, :string
field :latitude, :decimal
field :longitude, :decimal
field :gospel_topic, :string
field :device, :string
field :visitor_id, :string
belongs_to :contact, Project.Contact
belongs_to :country, Project.Country
belongs_to :language, Project.Language
belongs_to :primary_channel, Project.PrimaryChannel
belongs_to :primary_channel_identifier, Project.PrimaryChannelIdentifier
timestamps
end
@required_fields ~w(first_name last_name city region postal latitude longitude gospel_topic device visitor_id)
@optional_fields ~w()
@doc """
Creates a changeset based on the `model` and `params`.
If no params are provided, an invalid changeset is returned
with no validation performed.
"""
def changeset(model, params \\ :empty) do
model
|> cast(params, @required_fields, @optional_fields)
end
end
from ex_machina.
I have a similar problem.
def factory(:role) do
%Role{
name: "Developer"
}
end
def factory(:slot) do
%Slot{
role_id: create(:role).id,
}
end
and
create(:role, role_id: create(:role_id)
creates two role which i don't want. Is this a bug or am i doing anything wrongly ?
from ex_machina.
@dineshba In this case that is mistake in the code. When creating associations you should set the association, not the association's id. Also you should only call build
inside the factory otherwise there will indeed two records created. So what it should look like is:
def factory(:role) do
%Role{
name: "Developer"
}
end
def factory(:slot) do
%Slot{
role: build(:role)
}
end
If you have the appropriate relations set up in your models (has_many, belongs_to, has_one, etc.) then ExMachina will know how to insert the association when you call `create(:role, role: create(:role, name: "Whatever"))
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.