Laravel-permission: Error on migration.

Created on 18 Oct 2019  路  11Comments  路  Source: spatie/laravel-permission

lomas@lomas-S551LN:/var/www/html/imagingtrade.com.np$ php artisan migrate
Migrating: 2019_10_18_124032_create_permission_tables

Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name '' (SQL: create table `(idbigint unsigned not null auto_increment primary key,namevarchar(255) not null,guard_namevarchar(255) not null,created_attimestamp null,updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

at /var/www/html/imagingtrade.com.np/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
661| // If an exception occurs when attempting to run a query, we'll format the error
662| // message to include the bindings with SQL, which will make this exception a
663| // lot more helpful to the developer instead of just the database's errors.
664| catch (Exception $e) {

665| throw new QueryException(
666| $query, $this->prepareBindings($bindings), $e
667| );
668| }
669|

Exception trace:

1 PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name ''")
/var/www/html/imagingtrade.com.np/vendor/laravel/framework/src/Illuminate/Database/Connection.php:453

2 PDO::prepare("create table `(idbigint unsigned not null auto_increment primary key,namevarchar(255) not null,guard_namevarchar(255) not null,created_attimestamp null,updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'")
/var/www/html/imagingtrade.com.np/vendor/laravel/framework/src/Illuminate/Database/Connection.php:453

Please use the argument -v to see more details.
lomas@lomas-S551LN:/var/www/html/imagingtrade.com.np$

support

Most helpful comment

@joshilomas Just clear the cache with php artisan config:cache and delete the table if already created in the database. and try again

All 11 comments

Given that the SQL errors in your examples are missing table names, I'm guessing your config/permission.php file is either damaged or not published.

return [

'models' => [

    /*
     * When using the "HasPermissions" trait from this package, we need to know which
     * Eloquent model should be used to retrieve your permissions. Of course, it
     * is often just the "Permission" model but you may use whatever you like.
     *
     * The model you want to use as a Permission model needs to implement the
     * `Spatie\Permission\Contracts\Permission` contract.
     */

    'permission' => Spatie\Permission\Models\Permission::class,

    /*
     * When using the "HasRoles" trait from this package, we need to know which
     * Eloquent model should be used to retrieve your roles. Of course, it
     * is often just the "Role" model but you may use whatever you like.
     *
     * The model you want to use as a Role model needs to implement the
     * `Spatie\Permission\Contracts\Role` contract.
     */

    'role' => Spatie\Permission\Models\Role::class,

],

'table_names' => [

    /*
     * When using the "HasRoles" trait from this package, we need to know which
     * table should be used to retrieve your roles. We have chosen a basic
     * default value but you may easily change it to any table you like.
     */

    'roles' => 'roles',

    /*
     * When using the "HasPermissions" trait from this package, we need to know which
     * table should be used to retrieve your permissions. We have chosen a basic
     * default value but you may easily change it to any table you like.
     */

    'permissions' => 'permissions',

    /*
     * When using the "HasPermissions" trait from this package, we need to know which
     * table should be used to retrieve your models permissions. We have chosen a
     * basic default value but you may easily change it to any table you like.
     */

    'model_has_permissions' => 'model_has_permissions',

    /*
     * When using the "HasRoles" trait from this package, we need to know which
     * table should be used to retrieve your models roles. We have chosen a
     * basic default value but you may easily change it to any table you like.
     */

    'model_has_roles' => 'model_has_roles',

    /*
     * When using the "HasRoles" trait from this package, we need to know which
     * table should be used to retrieve your roles permissions. We have chosen a
     * basic default value but you may easily change it to any table you like.
     */

    'role_has_permissions' => 'role_has_permissions',
],

'column_names' => [

    /*
     * Change this if you want to name the related model primary key other than
     * `model_id`.
     *
     * For example, this would be nice if your primary keys are all UUIDs. In
     * that case, name this `model_uuid`.
     */

    'model_morph_key' => 'model_id',
],

/*
 * When set to true, the required permission/role names are added to the exception
 * message. This could be considered an information leak in some contexts, so
 * the default setting is false here for optimum safety.
 */

'display_permission_in_exception' => false,

'cache' => [

    /*
     * By default all permissions are cached for 24 hours to speed up performance.
     * When permissions or roles are updated the cache is flushed automatically.
     */

    'expiration_time' => \DateInterval::createFromDateString('24 hours'),

    /*
     * The cache key used to store all permissions.
     */

    'key' => 'spatie.permission.cache',

    /*
     * When checking for a permission against a model by passing a Permission
     * instance to the check, this key determines what attribute on the
     * Permissions model is used to cache against.
     *
     * Ideally, this should match your preferred way of checking permissions, eg:
     * `$user->can('view-posts')` would be 'name'.
     */

    'model_key' => 'name',

    /*
     * You may optionally indicate a specific cache driver to use for permission and
     * role caching using any of the `store` drivers listed in the cache.php config
     * file. Using 'default' here means to use the `default` set in cache.php.
     */

    'store' => 'default',
],

];

This is very unusual, and seems to be something in your environment.

You may want to explore whether any other packages encounter similar issues, including those which allow you to configure table names in a config file, instead of being directly coded into the migration.

I have the same problem.
Config file is there.

Migrating: 2019_10_19_213919_create_permission_tables

   Illuminate\Database\QueryException  : SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name '' (SQL: create table `` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `guard_name` varchar(255) not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci')

  at /var/www/example.app/htdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:665
    661|         // If an exception occurs when attempting to run a query, we'll format the error
    662|         // message to include the bindings with SQL, which will make this exception a
    663|         // lot more helpful to the developer instead of just the database's errors.
    664|         catch (Exception $e) {
  > 665|             throw new QueryException(
    666|                 $query, $this->prepareBindings($bindings), $e
    667|             );
    668|         }
    669| 

  Exception trace:

  1   PDOException::("SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name ''")
      /var/www/example.app/htdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:453

  2   PDO::prepare("create table `` (`id` bigint unsigned not null auto_increment primary key, `name` varchar(255) not null, `guard_name` varchar(255) not null, `created_at` timestamp null, `updated_at` timestamp null) default character set utf8mb4 collate 'utf8mb4_unicode_ci'")
      /var/www/example.app/htdocs/vendor/laravel/framework/src/Illuminate/Database/Connection.php:453

  Please use the argument -v to see more details.

Laravel version is 6.0
Any ideas?

Was able to fix it.
Uninstalled the package, cleared the cache, manually removed the permission table. After that the migration was working.

In this Application, I had not installed any other packages except laravel default ones.
I had completely uninstalled the package and reinstall but still not solved.

@joshilomas Just clear the cache with php artisan config:cache and delete the table if already created in the database. and try again

@joshilomas Just clear the cache with php artisan config:cache and delete the table if already created in the database. and try again

Thank You...Clearing Cache and completely reinstalling it...worked..

Just clearing the cache and deleting the table already created will solve the problem. no need to uninstall whole package

only clear cache it will work :)
=> php artisan clear:cache

In my project i have permission.php file
But i Can not migrate..

Migrating: 2020_05_17_230935_create_permission_tables

Exception : Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding.

at G:\newton\database\migrations\2020_05_17_230935_create_permission_tables.php:20
16| $tableNames = config('permission.table_names');
17| $columnNames = config('permission.column_names');
18|
19| if (empty($tableNames)) {

20| throw new \Exception('Error: config/permission.php not found and defaults could not be merged. Please publish the package configuration before proceeding.');
21| }
22|
23| Schema::create($tableNames['permissions'], function (Blueprint $table) {
24| $table->bigIncrements('id');

Exception trace:

1 CreatePermissionTables::up()
G:\newton\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:392

2 Illuminate\Database\Migrations\Migrator::Illuminate\Database\Migrations{closure}()
G:\newton\vendor\laravel\framework\src\Illuminate\Database\Migrations\Migrator.php:401

Please use the argument -v to see more details.

Was this page helpful?
0 / 5 - 0 ratings