Id not retrieved by ecto after insert on a legacy mysql database

Hi, I have a legacy mysql database:

CREATE TABLE `joboffers` (
  `idJobOffers` int(11) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`idJobOffers`),

so I mapped it:

defmodule RagingBull.Models.Requirement do
  use Ecto.Schema
  import Ecto.Changeset
  alias RagingBull.Models.Planning
  alias RagingBull.Models.Contract
  alias RagingBull.Models.SecondmentProposal
  @primary_key {:id, :integer, [source: :idJobOffers, autogenerate: false]}
  schema "joboffers" do
    field :status, :boolean
    has_many :plannings, Planning, foreign_key: :requirement_id
    has_many :contracts, Contract, foreign_key: :requirement_id
    has_many :secondment_proposals, SecondmentProposal, foreign_key: :requirement_id
  def changeset(requirement, attrs \\ %{}) do
    |> cast(attrs, [:id, :establishment_id, :subjob_id, :justification_id, :status])
    |> cast_assoc(:plannings, with: &Planning.changeset/2)
    |> cast_assoc(:contracts, with: &Contract.changeset/2)
    |> cast_assoc(:secondment_proposals, with: &SecondmentProposal.changeset/2)
    |> validate_required([:establishment_id, :subjob_id, :justification_id])

When it gets inserted, it never retrieves the id which is a major issue when trying to save an entire tree of things starting from the requitement (with contracts, plannings, secondment proposals which also have secondment_proposal_availabilities ).
Am I doing something wrong in the mapping?

In the meanwhile I did find a way to map it correctly:

@primary_key {:idJobOffers, :id, autogenerate: true}
@derive {Phoenix.Param, key: :idJobOffers}
schema “joboffers” do

and now it’s retrieved.

 RagingBull.Repo.insert r                                   
[debug] QUERY OK db=3.0ms idle=860.7ms
INSERT INTO `joboffers` (`Establishment_idEstablishment`,`status`,`SubJobs_idSubJobs`) VALUES (?,?,?) [6, true, 1]
   __meta__: #Ecto.Schema.Metadata<:loaded, "joboffers">,
   address: nil,
   budgeted: nil,
   date_created: nil,
   establishment_id: 6,
   idJobOffers: 108,
   is_replacing: nil,
   justification_id: nil,
   more: nil,
   notification_sent: nil,
   status: true,
   subjob_id: 1,
   temp_requested: nil

I still have the problem of naming it id :slight_smile:

And finally the right solution:

@primary_key {:id, :id, source: :idJobOffers, autogenerate: true}

Seems the problem was the type wasn’t :id, and autogenerate: false