完整性约束违规:字段列表中的 1052 列“updated_at”不明确
Posted
技术标签:
【中文标题】完整性约束违规:字段列表中的 1052 列“updated_at”不明确【英文标题】:Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous 【发布时间】:2020-10-11 19:00:25 【问题描述】:App\User Model 文件内容:-
<?php
namespace App;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Tymon\JWTAuth\Contracts\JWTSubject;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable implements JWTSubject
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
public function khatmas()
return $this->hasMany('App\Khatma');
public function messages()
return $this->hasManyThrough('App\KhatmaRead','App\Khatma')
->select('reader_name','message','updated_at')
->where('message','!=',NULL);
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function getJWTIdentifier()
return $this->getKey();
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* @return array
*/
public function getJWTCustomClaims()
return [];
khatma_reads 迁移文件:-
$table->bigIncrements('id');
$table->unsignedBigInteger('khatma_id');
$table->foreign('khatma_id')->references('id')->on('khatmas')->onDelete('cascade');
$table->string('reader_name')->nullable();
$table->longText('message')->nullable();
$table->timestamps();
在 user->message() 方法中,当我将列“updated_at”或“created_at”放在 ->select 方法上时出现错误:-
"SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous (SQL: select `reader_name`, `message`, `updated_at`, `khatmas`.`user_id` as `laravel_through_key` from `khatma_reads` inner join `khatmas` on `khatmas`.`id` = `khatma_reads`.`khatma_id` where `message` is not null and `khatmas`.`user_id` in (1))"
当我从查询构建器中完全删除 ->select() 方法时,我得到了所有列的响应,包括 created_at 和 updated_at 列。
我该如何解决这个问题?
【问题讨论】:
【参考方案1】:那是因为您的 KhatmaRead
和 Khatma
模型有 update_at
列,并且在这里:
->select('reader_name','message','updated_at')
您没有指定从哪个表中获取该列,这很模糊。你应该这样做:
->select('reader_name','message','khatma_reads.updated_at')
【讨论】:
我试过返回错误“SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Khatma.updated_at' in 'field list' (SQL: selectreader_name
, message
, @987654328 @.updated_at
, khatmas
.user_id
as laravel_through_key
from khatma_reads
inner join khatmas
on khatmas
.id
= @9876543@37@.khatma_id
khatma_id
不是 null 和khatmas
.user_id
in (1))"
我还尝试使用“KhatmaRead.updated_at”返回错误“SQLSTATE[42S22]:未找到列:1054 '字段列表'中的未知列'KhatmaRead.updated_at'(SQL:选择reader_name
, message
, KhatmaRead
.updated_at
, khatmas
.user_id
as laravel_through_key
from khatma_reads
inner join khatmas
on khatmas
.id
id
= id
@986545 987654355@ 不为空且khatmas
.user_id
在 (1))"
这只是一个例子。您的模型名为KhatmaRead
,我不知道您的表的名称,您不应该输入模型名称而是表的名称。但似乎是khatma_reads
,所以你必须这样做khatma_reads.updated_at
它的工作,谢谢。我输入了 khatma_reads 表名,现在它工作正常“khatma_reads.updated_at”,我已经更新了我的问题并包含了 khatma_reads 迁移文件内容,您能否用“khatma_reads.updated_at”更新您的答案,以便我接受。
以上是关于完整性约束违规:字段列表中的 1052 列“updated_at”不明确的主要内容,如果未能解决你的问题,请参考以下文章
SQLSTATE [23000]:完整性约束违规:1052 列“created_at”在 order 子句中不明确 Laravel 5.5
Laravel Eloquent:违反完整性约束:1052 列“id”在 where 子句不明确
SQLSTATE[23000]:Laravel 中的完整性约束违规
Laravel 7 错误 - SQLSTATE [23000]:完整性约束违规:1048 列 'first_name' 不能为空