问题描述
我编写了一个在本地服务器上运行良好的查询。但是在生产中,它不起作用。这是查询。
Invoice::with('contact_fk','transactions')
->where('date','<=',$this->query_date)
->withSum('transactions','amount')
->havingRaw('COALESCE(transactions_sum_amount,0) < invoices.net_total')
->orderBy('due_date','asc');
错误如下:
sqlSTATE[42000]: Syntax error or access violation: 1463 Non-grouping field
'net_total' is used in HAVING clause (sql: select sum(`net_total`) as
aggregate from (select `invoices`.*,(select sum(`transaction_allocations`.`amount`)
from `transaction_allocations` where `invoices`.`id` = `transaction_allocations`.`doc_id`
and `transaction_allocations`.`doc_type` = App\Models\Invoice and
`transaction_allocations`.`deleted_at` is null) as `transactions_sum_amount` from
`invoices` where `date` <= 2021-07-27 having COALESCE(transactions_sum_amount,0) < net_total)
as `temp_table`)
网上的很多建议都围绕着制作'strict' => false
而展开,这是不可接受的,因为它避免了问题并且没有解决问题。
我该如何解决?我的要求是带上所有未付的发票。付款存储在名为 transactions
的单独表中。因此,所有发票的 net_total
大于此发票的 amount
交易总和。
根据要求,包括带有相关字段的两个表结构。它们具有多态关系。 transaction_allocation
表包含 doc_id
和 doc_type
字段,分别填充为 Apps\Models\Invoice
和 invoice_id
。关系的名称是 transactions
。
Schema::create('invoices',function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
$table->decimal('net_total',19,4);
});
Schema::create('transaction_allocations',function (Blueprint $table) {
$table->bigIncrements('id');
$table->foreignId('transaction_id')->nullable()->constrained();
$table->Nullablemorphs('doc');
$table->decimal('amount',4)->nullable();
});
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)