【问题标题】:query that worked in Laravel 5.2 give me error in Laravel 5.3在 Laravel 5.2 中工作的查询在 Laravel 5.3 中给我错误
【发布时间】:2016-12-31 10:40:04
【问题描述】:

此查询适用于 5.2:

    $galleries = Gallery::with(array(
            'images' => function ($query) {
                $query->orderBy('order', 'asc');
            }
        ))
        ->with('votes')
        ->leftJoin('votes', 'votes.votable_id', '=', 'gallery.id')
        ->selectRaw(
            'gallery.*, count(case votes.status when "upvote" then 1 else null end) - count(case votes.status when "downvote" then 1 else null end) as points'
        )
        ->where('votes.votable_type','App\Gallery')
        ->groupBy('gallery.id')
        ->orderBy('points', 'desc')
        ->published()->orderBy('gallery.created_at', 'desc')->paginate(30);

我正在尝试选择所有有投票的画廊,当我在 5.3 中运行它时,我得到了这个

 1/2 PDOException in Connection.php line 333: SQLSTATE[42000]: 
Syntax error or access violation: 1055 'images.gallery.title' isn't in GROUP BY 

SQLSTATE[42000]: Syntax error or access violation: 1055 'images.gallery.title' isn't in 
GROUP BY (SQL: select gallery.*, count(case votes.status when "upvote" then 1 else null end) - count(case votes.status when "downvote" then 1 else null end) 
as points from `gallery` left join `votes` on `votes`.`votable_id` = `gallery`.`id`
where `votes`.`votable_type` = App\Gallery and `published` = 1 group by `gallery`.`id` 
order by `points` desc, `gallery`.`created_at` desc limit 30 offset 0)

【问题讨论】:

  • 您是否尝试将任务gallery.title 添加到GROUP BY 子句中?
  • 是的,我试过了,然后它告诉我在我的画廊表中添加所有列,所以我最终得到了这个:->groupBy('gallery.id', 'gallery.title', 'gallery.hash', 'gallery.user_id', 'gallery.published', 'gallery.views' , 'gallery.created_at', 'gallery.updated_at') 但这对我来说不合适。你觉得对吗?
  • 文档在升级指南中提到 5.3 已重写 join方法。 laravel.com/docs/5.3/upgrade#upgrade-5.3.0

标签: laravel eloquent laravel-5.2 laravel-5.3


【解决方案1】:

转到您的 config/database.php 文件夹。在mysql配置数组中,将strict => true改成strict => false,一切正常。

【讨论】:

    【解决方案2】:

    根据文档,您的连接查询应该是这样的 参考-:https://laravel.com/docs/5.3/upgrade#upgrade-5.3.0

     $galleries = Gallery::with(array(
            'images' => function ($query) {
                $query->orderBy('order', 'asc');
            }
        ))
        ->with('votes')
        ->leftJoin('votes',function($query){
                     $query->on('votes','gallery')->where('votes', 'votes.votable_id', '=', 'gallery.id')->where('votes.votable_type','App\Gallery');   })->selectRaw(
            'gallery.*, count(case votes.status when "upvote" then 1 else null end) - count(case votes.status when "downvote" then 1 else null end) as points'
        )
        ->groupBy('gallery.id')
        ->orderBy('points', 'desc')
        ->published()->orderBy('gallery.created_at', 'desc')->paginate(30);
    

    【讨论】:

    • 嗨 Hamza,运行时我收到此错误:SQLSTATE[42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误;查看与您的 MariaDB 服务器版本相对应的手册,了解在 'gallery.id votes = 附近使用的正确语法吗? votes.votable_type = 哪里?和published = ? grou' 在第 1 行(SQL:从 gallery 左连接 votes votes = gallery gallery.id votes = votes.votable_id 作为聚合选择 count(*) votes.votable_type = App\Gallery and published = 1 group by gallery.id) 有什么想法吗?
    • 我在查询上运行 toSql(),这是它产生的 sql:"select gallery.*, count(case votes.status when "upvote" then 1 else null end) - count(case votes.status 当“downvote”然后 1 else null end)作为来自gallery的点左加入votesvotes=gallerygallery.idvotes=?其中votes.votable_type=?和published = ? group by gallery.id order by points desc, gallery.created_at desc"
    • 我已经更新了答案试试看是否得到你想要的输出
    • 这是正确的做法 ->leftJoin('votes',function($query){ $query->on('votes.votable_id','gallery.id'); 它产生与 5.2 相同的查询,但我仍然得到 'images.gallery.title' 不在 GROUP BY 中。我在此处报告了此内容github.com/laravel/framework/issues/… 阅读更多内容,如果您有任何想法,请提供输入,谢谢 :)跨度>
    • 问题已解决,laravel 5.3 的 'strict' => true,在 config/database.php 中打开 ONLY_FULL_GROUP_BY 时查询导致错误
    猜你喜欢
    • 1970-01-01
    • 2017-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-02-04
    • 2017-04-22
    • 2017-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多