簡體   English   中英

PG::NotNullViolation: 錯誤:null 列“id”中的值違反了非空約束

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

當我嘗試添加新案例時出現此錯誤

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"

最近我從 heroku 克隆了我的應用程序,然后從那里提取了所有數據庫,然后當我添加一個帶有新列的新遷移時,我得到了錯誤

 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

在我的架構中

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

讓我知道您是否需要更多信息。 提前致謝。

該遷移似乎創建了一個null: false約束。 據推測,您數據庫中的現有行在那里沒有任何值。 要解決此問題,您必須能夠將一個選項傳遞給遷移以告訴它您希望該列可以為空。

你在用回形針嗎? Paperclip 已被棄用,取而代之的是 activestorage,但它的一個未解決的錯誤t.attachment遷移沒有正確傳遞選項,因此即使您確實使用了null: true選項,它也可能無法工作。 在這種情況下,解決方案是移動到活動存儲,或使用最新的回形針叉(回形針問題中推薦了一個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM