thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

'Base table or view not found' when making a belongsToMany relation and using laravel table prefix #3273

Open SteamWind opened 6 years ago

SteamWind commented 6 years ago

Description:

There is a bug with pivot table with prefix on display, it prefix the already prefixed table. Which causing this error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mydatabase.myprefix_myprefix_mytable' doesn't exist [...]

When we create the relationship, we need to put the prefix out of the name of the pivot table. I did something locally but it doesn't fix the bug at the core (I tried to fix it at the core but I don't have the level). I changed this line here and this other line with this on the creation of the belongsToMany relation:

belongsToMany($options->model, str_replace(\DB::getTablePrefix(), '', $options->pivot_table)

We could fix the problem at the core but it changes a lot of thing and I don't think it's really the right solution because we should save the pivot table without prefix in the database as we store all other tables with the prefix. I don't find it consistent.

Let me know what you think about that, I'm open to any suggestions. If this fix is okay for you, I'll open a PR.

Steps To Reproduce:

  1. Create a laravel project
  2. Configure table prefix
  3. Create a voyager project on it
  4. Create 3 tables (by exemple with 'prefix_' as prefix:
    • prefix_table1
    • prefix_table2
    • pivot table for table1 and table2 "prefix_table1_table2" (make it conventionally)
  5. Create bread for table1 and table2
  6. Add relation belongsToMany on table1 to table2
  7. Try to view table1 BREAD

You should reproduce this error:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mydatabase.prefix_prefix_table1_table2' doesn't exist [...]

SteamWind commented 6 years ago

Okay, saving table prefix in database causes a lot of problems, after using my fix, it seems to have a lot of place where we can't use the table name with the prefix. We need to save the table name without prefix. I tested to use the relations without the prefix saved in the DB and it seems to work everywhere.