我正在尝试向我的模型添加唯一验证,但当我尝试更新数据时出现错误。
桌子:
acq_m_budgets
==================================
budget_id serial NOT NULL,
budget_code character varying(15) NOT NULL,
budget_name character varying(100) NOT NULL,
ma_code character varying(10),
start_period timestamp without time zone NOT NULL,
end_period timestamp without time zone NOT NULL,
budget numeric(16) DEFAULT 0,
credit numeric(16) DEFAULT 0,
debet numeric(16) DEFAULT 0,
balance numeric(16) DEFAULT 0,
reserve numeric(16) DEFAULT 0,
created_by character varying(100) NOT NULL,
created_on timestamp without time zone DEFAULT now(),
updated_by character varying(100) NOT NULL,
updated_on timestamp without time zone DEFAULT now(),
CONSTRAINT PK_AcqMBudgets PRIMARY KEY (budget_id),
CONSTRAINT UN_AcqMBudgets UNIQUE (budget_code)
我的模型:AcqMBudgets.php
class AcqMBudgets extends Model
{
public $timestamps = false;
protected $primaryKey = 'budget_id';
public $sortable = ['budget_code', 'budget_name', 'ma_code', 'balance', 'updated_on'];
protected $fillable = ['budget_code', 'budget_name', 'ma_code', 'start_period', 'end_period', 'budget', 'credit', 'debet', 'balance', 'reserve', 'created_by', 'created_on', 'updated_by', 'updated_on'];
protected $attributes = [
'budget' => 0,
'credit' => 0,
'debet' => 0,
'balance' => 0,
'reserve' => 0,
];
在模型上,我已经将创建和更新方法的规则分开了。不同之处在于 updateRules() 中,规则数组中需要一个主键参数。
在控制器的update功能上,出现错误,指出:SQLSTATE[42703]: Undefined column: 7 ERROR: column "id" does not exist LINE 1: ...from "acq_m_budgets" where "budget_code" = $1 and "id" <> $2 ^ (SQL: select count(*) as aggregate from "acq_m_budgets" where "budget_code" = N01 and "id" <> )。
我使用的主键是整数和增量,但由于某些情况,主键的名称不能只是id,所以我将其更改为budget_id并已在模型开头声明它。根据错误消息,Laravel 似乎一直在尝试与该id字段而不是我声明的字段进行比较。需要做什么来解决这个问题?
MYYA