违反完整性约束:1052 字段列表中的列 'updated_at' 不明确
Integrity constraint violation: 1052 Column 'updated_at' in field list is ambiguous
App\User 模型文件的内容:-
<?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 列。
我该如何解决这个问题?
那是因为您的 KhatmaRead
和 Khatma
模型有 update_at
列,这里:
->select('reader_name','message','updated_at')
您没有指定 table 您将从哪个栏目中获取该栏目,这是不明确的。你应该这样做:
->select('reader_name','message','khatma_reads.updated_at')
App\User 模型文件的内容:-
<?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 列。
我该如何解决这个问题?
那是因为您的 KhatmaRead
和 Khatma
模型有 update_at
列,这里:
->select('reader_name','message','updated_at')
您没有指定 table 您将从哪个栏目中获取该栏目,这是不明确的。你应该这样做:
->select('reader_name','message','khatma_reads.updated_at')