laravel-multitenancy
laravel-multitenancy copied to clipboard
Possible incompatibility with Laravel Starter Kit (React) composer run dev
Hey team,
I’m using laravel-multitenancy together with the Laravel Starter Kit, and I’m running into an issue when executing composer run dev.
Here’s the error I get in the console:
Illuminate\Database\QueryException
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "cache" does not exist
LINE 1: select * from "cache" where "key" in ($1)
^ (Connection: tenant, SQL: select * from "cache" where "key" in (laravel_cache_laravel-data.config))
This leads me to suspect there might be some incompatibility or unexpected behavior between the multitenancy package and how the Laravel Starter Kit bootstraps during composer run dev.
This is the content of the dev script:
"dev": [
"Composer\\Config::disableProcessTimeout",
"npx concurrently -c \"#93c5fd,#c4b5fd,#fb7185,#fdba74\" \"php artisan serve\" \"php artisan queue:listen --tries=1\" \"php artisan pail --timeout=0\" \"npm run dev\" --names=server,queue,logs,vite"
],
Let me know if you need more context or if there’s a known workaround. Happy to help investigate further!
Please create a repository to check the issue.
Thanks.
Hello, did you find a solution to that?
For anyone running into this issue, here's my solution. When using session and cache on the database (this is default), you need to set .env variables otherwise it looks into the landlord database and not the tenant database.
So you need to add these two .env variables:
SESSION_CONNECTION=tenant
DB_CACHE_CONNECTION=tenant
@freekmurze First of all, thanks for making this package! And if this is the correct solution for solving the mentioned problem, it would be good to add this to the docs of the package. I've been over the docs 3 times and I haven't seen this mentioned.
Actually, the above solves the session and cache issue but doesn't solve the queue issue.
@freekmurze I don't know if I'm missing something obvious but have checked the docs and your talks (they're awesome), but in there I see that the default tables of session, cache and jobs are in the tenant databases. Is that right? If so, how to run the queues because they are not tenant aware because there's no host right?
Thanks!
We run Session, Cache and Job tables in the landlord table and all jobs etc are TenantAware by default.
@kieranbarlow Thanks for your reply! And yes, this is also how we've done it in the end.
Do you also have the users in the landlord table? And then a pivot table with the tenants they belong to?
Ps. Was bit confusing because in this talk of @freekmurze you can see that the Session, Cache and Jobs are in the tenant table.
No worries :)
I guess it depends how you want to set it up really, I've seen both implementations. We currently run users in each tenant database so they are completely isolated (currently in the process of setting up Entra ID and this approach has made it simpler to implement).
For me it makes sense having jobs in the landlord database so you can have X number of workers watching one queue rather than trying to loop through X number of tenants to check each job table.
@kieranbarlow Nice, thanks! Yes make lot of sense to do Entra ID, that way users don't have issues like different passwords etc per tenant.