Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

joinSub bug with table prefix #28391

Closed
kndol opened this issue May 2, 2019 · 2 comments · Fixed by #28400
Closed

joinSub bug with table prefix #28391

kndol opened this issue May 2, 2019 · 2 comments · Fixed by #28400
Labels

Comments

@kndol
Copy link

kndol commented May 2, 2019

  • Laravel Version: 5.8.15
  • PHP Version: 7.3.1p1
  • Database Driver & Version: mariadb 10.1.13

Description:

joinSub, leftJoinSub, and rightJoinSub functions have a bug with a table prefix.

Steps To Reproduce:

Set the table prefix as 'prefix_'.

        $selectedQuest = DB::table('quest_items')
            ->select('quest_item_id', 'content_no',
                'content_type', 'subject', 'content')
            ->where('quest_set_id', $questSetId)
            ->orderBy('quest_item_id');
       
        $questPlayerData = DB::table('quest_play_items')
            ->leftJoinSub($selectedQuest, 'selectedQuest', function ($join)
            {
                $join->on('quest_play_items.quest_item_id', 'selectedQuest.quest_item_id');
            })
            ->where('quest_play_items.quest_play_set_id', $questPlaySetId)->get();

Then error has occurred:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'prefix_selectedQuest.quest_item_id' in 'on clause' (SQL: select * from prefix_quest_play_items left join (select quest_item_id, content_no, content_type, subject, content from prefix_quest_items where quest_set_id = 1 order by quest_item_id asc) as selectedQuest on prefix_quest_play_items.quest_item_id = prefix_selectedQuest.quest_item_id where prefix_quest_play_items.quest_play_set_id = 28) {"exception":"[object] (Illuminate\Database\QueryException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'prefix_selectedQuest.quest_item_id' in 'on clause' (SQL: select * from prefix_quest_play_items left join (select quest_item_id, content_no, content_type, subject, content from prefix_quest_items where quest_set_id = 1 order by quest_item_id asc) as selectedQuest on prefix_quest_play_items.quest_item_id = prefix_selectedQuest.quest_item_id where prefix_quest_play_items.quest_play_set_id = 28) at /vendor/laravel/framework/src/Illuminate/Database/Connection.php:664, Doctrine\DBAL\Driver\PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'prefix_selectedQuest.quest_item_id' in 'on clause' at /vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:63, PDOException(code: 42S22): SQLSTATE[42S22]: Column not found: 1054 Unknown column 'prefix_selectedQuest.quest_item_id' in 'on clause' at /vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/PDOConnection.php:61)

So we need to explicitly add a prefix to the alias to make the function work properly:

        $selectedQuest = DB::table('quest_items')
            ->select('quest_item_id', 'content_no',
                'content_type', 'subject', 'content')
            ->where('quest_set_id', $questSetId)
            ->orderBy('quest_item_id');
       
        $questPlayerData = DB::table('quest_play_items')
            ->leftJoinSub($selectedQuest, '**prefix_**selectedQuest', function ($join)
            {
                $join->on('quest_play_items.quest_item_id', 'selectedQuest.quest_item_id');
            })
            ->where('quest_play_items.quest_play_set_id', $questPlaySetId)->get();
@staudenmeir
Copy link
Contributor

I'll fix it.

@driesvints driesvints added the bug label May 2, 2019
@driesvints
Copy link
Member

@staudenmeir thanks for looking into this 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants