Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

curtsheller's avatar

SQLite/MySQL error using Spatie Roles and Permissions.

Spent a couple of days on this and can't figure this out (chatGPT no help, Spatie site no help, and non githug discussions I can find).

Using straight up Spatie Roles and Permissions not changes to base package.. Getting this error with Spatie Roles & Permissions v6: "select "roles".*, "permission"."table_names"."model_has_roles"."model_id" as "pivot_model_id", "permission"."table_names"."model_has_roles"."role_id" as "pivot_role_id", "permission"."table_names"."model_has_roles"."model_type" as "pivot_model_type" from "roles" inner join "permission"."table_names"."model_has_roles" on "roles"."id" = "permission"."table_names"."model_has_roles"."role_id" where "permission"."table_names"."model_has_roles"."model_id" in (1, 7, 12, 14, 15, 18, 23) and "permission"."table_names"."model_has_roles"."model_type" = App\Models\User"

Happens with SQLite and MySQL.

SQLite error: "SQLSTATE[HY000]: General error: 1 near ".": syntax error"

MySQL error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.model_id as pivot_model_id, permission.table_names.model_has_roles.`r' at line 1

Any help or tips for tracking down/debuggin the problem would help.

Curt Sheller

0 likes
5 replies
LaryAI's avatar
Level 58

The error you're encountering seems to be related to incorrect SQL syntax, particularly with the way table names and columns are being referenced. This is likely due to a misconfiguration or a bug in the way the Spatie package is being used or set up. Here are some steps you can take to troubleshoot and potentially resolve the issue:

  1. Check Configuration: Ensure that your config/permission.php file is correctly set up. The default table names should be roles, permissions, model_has_roles, model_has_permissions, and role_has_permissions. If you've customized these, make sure they are correctly reflected in your configuration.

  2. Database Migrations: Verify that your database migrations have been run successfully and that the tables exist with the correct structure. You can check this by looking at your database directly or by running the migrations again using:

    php artisan migrate
    
  3. Clear Cache: Sometimes, cached configurations or routes can cause issues. Clear your application cache using:

    php artisan config:cache
    php artisan cache:clear
    php artisan route:clear
    php artisan view:clear
    
  4. Check for Overridden Models: If you've overridden the default Role or Permission models provided by Spatie, ensure that they are correctly set up and that any relationships are properly defined.

  5. Debugging SQL Queries: Enable query logging to see the exact SQL being executed. You can do this by adding the following to your AppServiceProvider:

    use Illuminate\Support\Facades\DB;
    use Illuminate\Support\Facades\Log;
    
    public function boot()
    {
        DB::listen(function ($query) {
            Log::info($query->sql);
            Log::info($query->bindings);
            Log::info($query->time);
        });
    }
    

    This will log all SQL queries to your log file, which can help you identify where the syntax error is occurring.

  6. Check for Package Updates: Ensure that you are using the latest version of the Spatie package. There might be bug fixes or improvements that address your issue. You can update the package using:

    composer update spatie/laravel-permission
    
  7. Review Custom Code: If you have any custom code that interacts with roles and permissions, review it to ensure that it is not causing the issue. Pay special attention to any custom queries or model relationships.

If none of these steps resolve the issue, consider creating a minimal reproducible example and posting it on the Laracasts forum or GitHub issues page for the Spatie package. This will help others understand the problem and provide more targeted assistance.

curtsheller's avatar

@LaryAI 1 thru 6, did all that. AN, 7, I have no custom code as far as the Spatie package, using it straight os advertised.

I don;t understand the "table_names" in the select command.

curtsheller's avatar

@jlrdw Using straight up Spatie Roles & Permissions v6 no changes to base package or congif/permissions.php table names, using the defaults... Getting this error with : "select "roles".*, "permission"."table_names"."model_has_roles"."model_id" as "pivot_model_id", "permission"."table_names"."model_has_roles"."role_id" as "pivot_role_id", "permission"."table_names"."model_has_roles"."model_type" as "pivot_model_type" from "roles" inner join "permission"."table_names"."model_has_roles" on "roles"."id" = "permission"."table_names"."model_has_roles"."role_id" where "permission"."table_names"."model_has_roles"."model_id" in (1, 7, 12, 14, 15, 18, 23) and "permission"."table_names"."model_has_roles"."model_type" = App\Models\User"

I don't understand the "table_names" in the "select "roles".*, "permission"."table_names"."model_has_roles"."model_id" as "pivot_model_id"..." Something is not getting set as I believe the table_names should not be there.

Please or to participate in this conversation.