nourza
nourza

Reputation: 2321

PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint

I am getting this error when I try to add a new case

PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint DETAIL: Failing row contains (null, nini, momo, 2023-01-21, 2023-01-31, rwe, rwe, Upper, 18, Report_Cards_-_K-9_Three_Term.pdf, application/pdf, 847591, 2023-01-27 01:46:36.772805, ewr, werr, 449, f, f, f, T4-1007-0001_2020.pdf, application/pdf, 60955, 2023-01-27 01:46:36.803862, null, null, null, null, null, null, null, null, null, null, null, null, 2023-01-27 01:46:36.867886, 2023-01-27 01:46:36.867886). : INSERT INTO "cases" ("pt_first_name", "pt_last_name", "date_received", "due_date", "ship", "shade", "finished", "outsourced", "mould", "upper_lower", "implant_brand", "implant_quantity", "invoice_file_name", "invoice_content_type", "invoice_file_size", "invoice_updated_at", "invoice2_file_name", "invoice2_content_type", "invoice2_file_size", "invoice2_updated_at", "number", "user_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24) RETURNING "id"

Recently I clone my app from heroku and I pull all the database from there then when I added a new migration with new column I got the error

 def self.up
    change_table :cases do |t|
      t.attachment :invoice2
      t.attachment :invoice3
      t.attachment :invoice4
      t.attachment :invoice5
      t.timestamps
    end
  end

  def self.down
    remove_attachment :cases, :invoice2
    remove_attachment :cases, :invoice3
    remove_attachment :cases, :invoice4
    remove_attachment :cases, :invoice5
  end

in my schema

create_table "cases", force: :cascade do |t|
    t.string   "pt_first_name"
    t.string   "pt_last_name"
    t.date     "date_received"
    t.date     "due_date"
    t.string   "shade"
    t.string   "mould"
    t.string   "upper_lower"
    t.integer  "user_id"
    t.string   "invoice_file_name"
    t.string   "invoice_content_type"
    t.integer  "invoice_file_size",     limit: 8
    t.datetime "invoice_updated_at"
    t.string   "implant_brand"
    t.string   "implant_quantity"
    t.integer  "number"
    t.boolean  "finished"
    t.boolean  "ship"
    t.boolean  "outsourced"
    t.string   "invoice2_file_name"
    t.string   "invoice2_content_type"
    t.integer  "invoice2_file_size",    limit: 8
    t.datetime "invoice2_updated_at"
    t.string   "invoice3_file_name"
    t.string   "invoice3_content_type"
    t.integer  "invoice3_file_size",    limit: 8
    t.datetime "invoice3_updated_at"
    t.string   "invoice4_file_name"
    t.string   "invoice4_content_type"
    t.integer  "invoice4_file_size",    limit: 8
    t.datetime "invoice4_updated_at"
    t.string   "invoice5_file_name"
    t.string   "invoice5_content_type"
    t.integer  "invoice5_file_size",    limit: 8
    t.datetime "invoice5_updated_at"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

Let me know if there more information do you need! Thanks in advance.

Upvotes: 0

Views: 704

Answers (1)

iftheshoefritz
iftheshoefritz

Reputation: 6129

That migration seems like it creates a null: false constraint. Presumably the existing rows in your DB do not have any values there. To fix this you would have to be able to pass an option to the migration to tell it that you want the column to be nullable.

Are you using Paperclip? Paperclip has been deprecated in favor of activestorage, but one of its open bugs was that the t.attachment migration didn’t pass on options correctly, so even if you did use a null: true option, it might not work. In this case the solution would be to move to active storage, or use a fork of paperclip that is up to date (there is one recommended in that paperclip issue.

Upvotes: 1

Related Questions