Laravel-Backpack / CRUD

Build custom admin panels. Fast!
https://backpackforlaravel.com
MIT License
3.08k stars 886 forks source link

[Bug] Query builder not properly managing database table prefix #4753

Closed lzampieri closed 1 year ago

lzampieri commented 1 year ago

Bug report

Conditions

The bug appears when prefix is valued in config/database.php.

In that situation, trying to access the dashboard interface of a CRUD model, an error is obtained, similar to: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'prefix_prefix_tablename.id' in 'field list' (SQL: select count(*) as total_rows from (select `prefix_prefix_tablename`.`id` from `prefix_tablename`) as `prefix_prefix_users_aggregator`)

Note the double prefix appearing in the column name, which cause the error.

Fix

The bug seems to be solvable replacing the line 236 in vendor\backpack\crud\src\app\Library\CrudPanel\Traits\Query.php; in particular, inside private function getCountFromQuery(Builder $query), one should replace:

$modelTable = $this->model->getTableWithPrefix();

with

$modelTable = $this->model->getTable();

However, tests should be carried out to confirm that there are no counter-effects.

Versioning

When I run php artisan backpack:version the output is:

### PHP VERSION:
PHP 7.3.33 (cli) (built: Nov 16 2021 14:49:44) ( ZTS MSVC15 (Visual C++ 2017) x64 )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.3.33, Copyright (c) 1998-2018 Zend Technologies

### LARAVEL VERSION:
v8.83.25@b77b908a9426efa41d6286a2ef4c3adbf5398ca1

### BACKPACK VERSION:
5.4.6@39876b312b1ef3d6faebba89a0220a6bed7f870c

Thanks for the great work! Have a nice day.

welcome[bot] commented 1 year ago

Hello there! Thanks for opening your first issue on this repo!

Just a heads-up: Here at Backpack we use Github Issues only for tracking bugs. Talk about new features is also acceptable. This helps a lot in keeping our focus on improving Backpack. If you issue is not a bug/feature, please help us out by closing the issue yourself and posting in the appropriate medium (see below). If you're not sure where it fits, it's ok, a community member will probably reply to help you with that.

Backpack communication channels:

Please keep in mind Backpack offers no official / paid support. Whatever help you receive here, on Gitter, Slack or Stackoverflow is thanks to our awesome awesome community members, who give up some of their time to help their peers. If you want to join our community, just start pitching in. We take pride in being a welcoming bunch.

Thank you!

-- Justin Case The Backpack Robot

pxpm commented 1 year ago

Thanks @lzampieri

I can confirm the bug! QueryBuilder automatically adds the prefix in the table when doing the query we don't need to provide it.

I've just submitted #4754 that fixes it, should be merged and tagged later today.

Thanks for the report, and sorry for the bad experience! 🙏

Closing this in favor of the PR.

Cheers