嘗試從控制器創建嘗試時遇到ActiveRecord::NotNullViolation Exception: PG::NotNullViolation: ERROR: null value in column "item_id" violates not-null constraint錯誤。我不知道如何解決這個錯誤。任何幫助,將不勝感激。我嘗試谷歌搜索無濟于事。paper_trail2019 年 gem 的存盤庫中出現了一個問題pedrofurtado,建議洗掉gem 'activerecord-suppress_range_error'我沒有使用的。
我的控制器看起來像 -
# frozen_string_literal: true
class AttemptsController < ApplicationController
before_action :set_training
before_action :set_attempt, only: [:update]
def new
@attempt = Attempt.new
@questions = @training.questions
skip_authorization
end
def create
@attempt = @training.attempts.new(attempt_params)
@attempt.option_id << attempt_params[:option_id]
byebug
@attempt.save
skip_authorization
end
private
def attempt_params
params.require(:attempt).permit(:question_id, :user_id, :option_id)
end
def set_attempt
@attempt = Attempt.find(params[:id])
end
def set_training
@training = Training.find(params[:training_id])
end
end
移民 -
# frozen_string_literal: true
class CreateAttempts < ActiveRecord::Migration[6.1]
def change
create_table :attempts, type: :uuid do |t|
t.references :user, type: :uuid, index: true, foreign_key: true, null: false
t.references :training, type: :uuid, index: true, foreign_key: true, null: false
t.string :option_id, array: true, default: [], null: false
t.string :question_id, null: false
t.timestamps
end
end
end
我的導軌版本是6.1.3. Ruby 版本是2.7.5
Paper trail gem 的版本是11.0
uj5u.com熱心網友回復:
我自己解決了這個問題。問題是未將 id 設定為 UUID 的嘗試表,并且item_id定義 的版本表拒絕更新它,因為當未保存嘗試實體時,它正在回滾。我沒有將 id 設定為 UUID,而是將表的型別設定為 UUID。
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/487402.html
