SELECT 列表的 1055 表达式 #3 不在 ManyToMany 关系中的 GROUP BY 子句错误中 laravel 5.3
1055 Expression #3 of SELECT list is not in GROUP BY clause error in ManyToMany relationship laravel 5.3
我有一个这样的Post
模型:
class Post extends Model
{
protected $primaryKey = 'post_id';
public function tags ()
{
return $this->belongsToMany('App\Tag');
}
}
和一个Tag
模特:
class Tag extends Model
{
public function posts ()
{
return $this->belongsToMany('App\Post');
}
public function tagsCount ()
{
return $this->belongsToMany('App\Post')
->selectRaw('count(pt_id) as count')
->groupBy('tag_id');
}
public function getTagsCountAttribute()
{
if ( ! array_key_exists('tagsCount', $this->relations)) $this->load('tagsCount');
$related = $this->getRelation('tagsCount')->first();
return ($related) ? $related->count : 0;
}
}
(pt_id
列是 post_tag
数据透视表 table 中的主键字段)。
如您所见,Post
和 Tag
模型之间存在 ManyToMany 关系。
对于特定 Post 的计数相关标签,我向 Tag
模型添加了 tagsCount()
和 getTagsCountAttribute()
方法。
现在假设我想像这样获取特定 Post 的标签计数:
$post = Post::find($post_id)->get();
return $post->tagsCount
它在 laravel 5.2(和旧版本)中对我有用,但升级到 laravel 5.3 后,显示以下错误:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'aids.post_tag.post_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select count(pt_id) as count, `post_tag`.`tag_id` as `pivot_tag_id`, `post_tag`.`post_id` as `pivot_post_id` from `posts` inner join `post_tag` on `posts`.`post_id` = `post_tag`.`post_id` where `post_tag`.`tag_id` in (145) and `posts`.`deleted_at` is null group by `post_tag`.`tag_id`)
什么是问题,我该如何解决?
这与mysql5.7
有关
长话短说,一种解决方案是尝试将 config/database.php
从 true 更改为 false:
'mysql' => [
'strict' => false, //behave like 5.6
//'strict' => true //behave like 5.7
],
有关详细信息,请参见此处:
我没有尝试@Ryan,但是通过添加 pt_id
(post_tag 主键字段 table),问题解决了:
public function tagsCount ()
{
return $this->belongsToMany('App\Post')
->selectRaw('count(pt_id) as count')
->groupBy(['tag_id', 'pt_id']);
}
我有一个这样的Post
模型:
class Post extends Model
{
protected $primaryKey = 'post_id';
public function tags ()
{
return $this->belongsToMany('App\Tag');
}
}
和一个Tag
模特:
class Tag extends Model
{
public function posts ()
{
return $this->belongsToMany('App\Post');
}
public function tagsCount ()
{
return $this->belongsToMany('App\Post')
->selectRaw('count(pt_id) as count')
->groupBy('tag_id');
}
public function getTagsCountAttribute()
{
if ( ! array_key_exists('tagsCount', $this->relations)) $this->load('tagsCount');
$related = $this->getRelation('tagsCount')->first();
return ($related) ? $related->count : 0;
}
}
(pt_id
列是 post_tag
数据透视表 table 中的主键字段)。
如您所见,Post
和 Tag
模型之间存在 ManyToMany 关系。
对于特定 Post 的计数相关标签,我向 Tag
模型添加了 tagsCount()
和 getTagsCountAttribute()
方法。
现在假设我想像这样获取特定 Post 的标签计数:
$post = Post::find($post_id)->get();
return $post->tagsCount
它在 laravel 5.2(和旧版本)中对我有用,但升级到 laravel 5.3 后,显示以下错误:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #3 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'aids.post_tag.post_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select count(pt_id) as count, `post_tag`.`tag_id` as `pivot_tag_id`, `post_tag`.`post_id` as `pivot_post_id` from `posts` inner join `post_tag` on `posts`.`post_id` = `post_tag`.`post_id` where `post_tag`.`tag_id` in (145) and `posts`.`deleted_at` is null group by `post_tag`.`tag_id`)
什么是问题,我该如何解决?
这与mysql5.7
有关长话短说,一种解决方案是尝试将 config/database.php
从 true 更改为 false:
'mysql' => [
'strict' => false, //behave like 5.6
//'strict' => true //behave like 5.7
],
有关详细信息,请参见此处:
我没有尝试@Ryan,但是通过添加 pt_id
(post_tag 主键字段 table),问题解决了:
public function tagsCount ()
{
return $this->belongsToMany('App\Post')
->selectRaw('count(pt_id) as count')
->groupBy(['tag_id', 'pt_id']);
}