Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sometimes dropping the primary key isn't desired #143

Closed
geofflane opened this issue Jun 25, 2016 · 1 comment
Closed

Sometimes dropping the primary key isn't desired #143

geofflane opened this issue Jun 25, 2016 · 1 comment

Comments

@geofflane
Copy link
Contributor

Found this problem using params_for, etc.

defp drop_ecto_fields(record = %{__struct__: struct, __meta__: %{__struct__: Ecto.Schema.Metadata}}) do
    ...
    |> Map.drop(struct.__schema__(:primary_key))

In the simple case the primary key is a synthetic id that is auto-generated by the database. In the not so simple case, you have overridden the primary key so that it's not auto-generated by the database. It could be a business key that is meaningful. It could be a one-to-one mapping where the primary key is a foreign key reference to another table.

e.g.

# This has a normal auto-incrementing ID
schema "plays" do
  has_one :scoring
end

# This is a one-to-one modeled in the database by having the PK be a FK to the plays.id column.
@primary_key {:play_id, :integer, []}
schema "scoring" do
  field :type, :string
  field :points, :integer

  belongs_to :play, MyApp.Play, define_field: false, references: :id
  belongs_to :team, MyApp.Team, references: :id
end

In the latter case, the primary key is also a foreign key reference to another table and is a required field, so using params_for and friends creates invalid params since it drops the PK.

@geofflane
Copy link
Contributor Author

Maybe only remove autogenerated ids? It looks like that can be done using __schema__(: autogenerate_id)

paulcsmith pushed a commit that referenced this issue Jul 5, 2016
Fixes #143. Drops only auto-generated ids and not custom IDs that are meaningful in the domain.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant