Duplicate key value


#1

Anyone knows why this controller code:

async createEmployee({ request, auth }) {

// Set user vars
let user = await User.create({
  firstname: request.post().employee_firstname,
  lastname: request.post().employee_lastname,
  role: request.post().role,
  depot_id: request.post().depot_id,
  email_address: request.post().employee_email_address,
  created_at: now,
  created_by: auth.user.id,
  updated_at: now,
  updated_by: auth.user.id
})

// Set employee vars
let employee = await Employee.create({
  user_id: user.toJSON().id,
  depot_id: request.post().depot_id,
  employee_title: request.post().employee_title,
  employee_address: request.post().employee_address,
  employee_zip: request.post().employee_zip,
  employee_city: request.post().employee_city,
  employee_phone: request.post().employee_phone,
  created_at: now,
  created_by: auth.user.id,
  locked_at: now,
  locked_by: auth.user.id,
  updated_at: now,
  updated_by: auth.user.id
})

return { status: 'success', newid: employee.toJSON().id }

}

gives me:

"insert into "users" ("created_at", "created_by", "depot_id", "email_address", "firstname", "lastname", "role", "updated_at", "updated_by") values ($1, $2, $3, $4, $5, $6, $7, $8, $9) returning "id" - duplicate key value violates unique constraint "users_pkey""

I’m using Postgres 9,5


#2

managed to identify the issue (manually inserting IDs in seeds broke sequencing in postgres)