tenancy / multi-tenant

Run multiple websites using the same Laravel installation while keeping tenant specific data separated for fully independent multi-domain setups, previously github.com/hyn/multi-tenant
https://tenancy.dev
MIT License
2.56k stars 393 forks source link

Error in the last step, the setup #192

Closed froid24 closed 7 years ago

froid24 commented 7 years ago

I am running wampserver 64 on my local machine, with php 7.1.3 and mysql 5.7.14.

Since this package is said to work with Laravel 5.3, I installed 5.3. Then followed all steps to install and configure multi-tenancy. When running the last command, to set up, I get an error. I've had the same error while trying to use Laravel 5.4 with the 3.x-dev version, but there exists a solution for fixing it in 5.4. However, I have found nothing for 5.3...

Here-s the output of that command:

php artisan multi-tenant:setup --customer=flux53C1 --email=froid24@yahoo.com --hostname=flux53t1 --webserver=apache Welcome to hyn multi tenancy. Copied Directory [\vendor\hyn\multi-tenant\database\migrations] To [\database\migrations] Publishing complete for tag []! Copied Directory [\vendor\hyn\multi-tenant\database\migrations] To [\database\migrations] Publishing complete for tag []!

You are now able to edit the published multi-tenant.php configuration file before continuing. Ready? (yes/no) [yes]:

y

First off, migrations for the packages will run. Not running tenancy migration, falling back on native laravel migrate command due to missing tenant option. Migration table created successfully.

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table tenants add index tenants_custo mer_no_name_index(customer_no, name))

[Doctrine\DBAL\Driver\PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes

luceos commented 7 years ago

3.x-dev is unstable, might even use volatile here.. Hardly any of the features are implemented. Related to your issue, you should read this, in your AppServiceProvider boot method:

Schema::defaultStringLength(191);

and import Schema at the top:

use Illuminate\Support\Facades\Schema;

image

froid24 commented 7 years ago

That was the solution I was talking about for 5.4, found it and worked on the 5.4 installation. But as I understand (correct me if I'm wrong), defaultStringLength is a function introduced in 5.4. If I try to do this on my 5.3 installation, I get:

php artisan multi-tenant:setup --customer=flux53C1 --email=froid24@yahoo.com --hostname=flux53t1 --webserver=apache

[Symfony\Component\Debug\Exception\FatalThrowableError] Call to undefined method Illuminate\Database\Schema\MySqlBuilder::defaultStringLength()

luceos commented 7 years ago

You should not use it in every version of Laravel. Because of that and the freedom the developer (and customer) has to decide on their own database driver I was against putting it in the package code by default. It's related to your app ánd your setup, not the package :)

froid24 commented 7 years ago

Ok. I see tables are created in hyn_multi_tenancy, but no initial tenant was added. This will be done only through code? or are there console commands for this?

Now since you've been so kind to answer my questions, can you please clarify a bit the relation between tenant and website. I am new to Laravel, so before I start learning, I would like to know that it is the right solution for my project. What I want to achieve is a SaaS app with just one codebase, but separate dabatases for tenants. So basically the "website" will be the same, but from the domain/subdomain the tenant database is selected and everything onward happens with his data only (users, roles, login, added packages, custom code I write etc.)

Is this the case with this package? Thank you so much for the help.

luceos commented 7 years ago

Yes this is possible with 2.x and the future 3.x too.

froid24 commented 7 years ago

One last question, this will probably shed some light on future things as well.

I created 2 tenants through code as described in the documentation. Configured the hosts, so they are accessible.

Then I added the auth scaffolding (php artisan make:auth). Then I went to each tenant host and registered a user. But when I look in the DB, both users are in the initial DB, not in the tenant db.users tables...

Am I doing something wrong? Did I not do something needed?

Thank you so much for the help.

luceos commented 7 years ago

Make sure your User model uses the tenant connection instead of th system one. Easiest way is changing the default connection to tenant. The other way is having the User class extens the TenantModel provided by this package or by implementing some logic of your own to force the connection on the model, eg.

froid24 commented 7 years ago

OK. chose the first option for now. Many thanks for the help!