Laravel-Backpack / community-forum

A workspace to discuss improvement and feature ideas, before they're actually implemented.
28 stars 0 forks source link

[Bug] Checking column exists in database doesn't work with MariaDB #920

Closed tringuyenduc2903 closed 2 months ago

tringuyenduc2903 commented 2 months ago

Bug report

What I did

Set config in .env file

DB_CONNECTION=mariadb // Laravel 11 already supports MariaDB connections instead of MySQL

What I expected to happen

Columns that are not in the database will be automatically set 'orderable' => false

What happened

https://github.com/Laravel-Backpack/PermissionManager/issues/357 https://github.com/Laravel-Backpack/activity-log/issues/39

What I've already tried to fix it

The hasDatabaseColumn mechanism does not work

    /**
     * The only REALLY MANDATORY attribute for a column is the 'name'.
     * Everything else, Backpack can probably guess.
     *
     * This method checks that all necessary attributes are set.
     * If not, it tries to guess them.
     *
     * @param string|array $column The column definition array OR column name as string.
     * @return array Proper column definition array.
     */
    public function makeSureColumnHasNeededAttributes($column)
    {
       ...
        // check if the column exists in the database (as a db column)
        $column_exists_in_db = $this->hasDatabaseColumn($this->model->getTable(), $column['name']);
       ...
        $column['orderable'] = $column['orderable'] ?? $column_exists_in_db;
       ...
    }
    /**
     * Check if the column exists in the database, as a DB column.
     *
     * @param  string  $table
     * @param  string  $name
     * @return bool
     */
    protected function hasDatabaseColumn($table, $name)
    {
       ...
        if (! $this->driverIsSql()) {
            return true;
        }
       ...
    }

Is it a bug in the latest version of Backpack?

After I run backpack composer update backpack/crud the error it still persists

Backpack, Laravel, PHP, DB version

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

### PHP VERSION:
8.3.4

### PHP EXTENSIONS:
Core, date, libxml, openssl, pcre, zlib, filter, hash, json, pcntl, random, Reflection, SPL, session, standard, sodium, mysqlnd, PDO, xml, bcmath, calendar, ctype, curl, dom, mbstring, FFI, fileinfo, ftp, gd, gettext, iconv, igbinary, imagick, imap, intl, ldap, exif, msgpack, mysqli, pcov, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, readline, redis, shmop, SimpleXML, soap, sockets, sqlite3, sysvmsg, sysvsem, sysvshm, tokenizer, xmlreader, xmlwriter, xsl, zip, memcached, swoole, Zend OPcache, xdebug

### LARAVEL VERSION:
11.2.0.0

### BACKPACK PACKAGE VERSIONS:
backpack/activity-log: 2.0.3
backpack/backupmanager: v5.0.2
backpack/basset: 1.3.0
backpack/crud: 6.7.5
backpack/filemanager: 3.0.7
backpack/generators: v4.0.5
backpack/language-switcher: 2.0.0
backpack/logmanager: v5.0.1
backpack/medialibrary-uploaders: 1.2.0
backpack/permissionmanager: 7.2.0
backpack/pro: 2.1.12
backpack/revise-operation: 2.0.0
backpack/theme-tabler: 1.2.8
jcastroa87 commented 2 months ago

Hello @tringuyenduc2903

I found the problem here and i will create a PR to fix it. Meanwhile, you can modify the file vendor/backpack/crud/src/app/Library/CrudPanel/CrudPanel.php, method getSqlDriverList and add "mariadb" to the array.

public function getSqlDriverList()
    {
        return ['mysql', 'sqlsrv', 'sqlite', 'pgsql', 'mariadb'];
    }

Cheers.

jcastroa87 commented 2 months ago

The PR is create: https://github.com/Laravel-Backpack/CRUD/pull/5490

I will close this issue, but please feel free to re-open or create a new one if needed.

Cheers.