archtechx / tenancy

Automatic multi-tenancy for Laravel. No code changes needed.
https://tenancyforlaravel.com
MIT License
3.67k stars 432 forks source link

On Tenant::factory()->create() no tenant migrations are migrated as tenant is always null #1261

Closed SuperDJ closed 1 month ago

SuperDJ commented 1 month ago

Bug description

On Tenant::factory()->create() no tenant migrations are migrated as tenant is always null in Stancl\Tenancy\Commands\Migrate.

Steps to reproduce

Expected behavior

When using Tenant::factory()->create() tenant migrations should be migrated.

Laravel version

11.26

stancl/tenancy version

3.8.5

stancl commented 1 month ago

Don't think this is a bug in the package. If you use a factory that's your own code, you'd need to point out what specifically is wrong in the package here.

SuperDJ commented 1 month ago

When I call $tenant = Tenant::factory()->create(); in a test I get the following error:

Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedById: Tenant could not be identified with tenant_id

This is comming from:

/vendor/stancl/tenancy/src/Tenancy.php:39 /vendor/stancl/tenancy/src/Tenancy.php:159 /vendor/stancl/tenancy/src/Commands/Migrate.php:51 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36 /vendor/laravel/framework/src/Illuminate/Container/Util.php:43 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:95 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:35 /vendor/laravel/framework/src/Illuminate/Container/Container.php:690 /vendor/laravel/framework/src/Illuminate/Console/Command.php:213 /vendor/symfony/console/Command/Command.php:279 /vendor/laravel/framework/src/Illuminate/Console/Command.php:182 /vendor/symfony/console/Application.php:1029 /vendor/symfony/console/Application.php:316 /vendor/symfony/console/Application.php:167 /vendor/laravel/framework/src/Illuminate/Console/Application.php:163 /vendor/laravel/framework/src/Illuminate/Foundation/Console/Kernel.php:426 /vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:358 /vendor/stancl/tenancy/src/Jobs/MigrateDatabase.php:34 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36 /vendor/laravel/framework/src/Illuminate/Container/Util.php:43 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:95 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:35 /vendor/laravel/framework/src/Illuminate/Container/Container.php:690 /vendor/stancl/jobpipeline/src/JobPipeline.php:68 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:36 /vendor/laravel/framework/src/Illuminate/Container/Util.php:43 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:95 /vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php:35 /vendor/laravel/framework/src/Illuminate/Container/Container.php:690 /vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:128 /vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:144 /vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php:119 /vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:132 /vendor/laravel/framework/src/Illuminate/Bus/Dispatcher.php:98 /vendor/laravel/framework/src/Illuminate/Foundation/helpers.php:444 /vendor/stancl/jobpipeline/src/JobPipeline.php:96 /vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:458 /vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:286 /vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php:266 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php:233 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php:208 /vendor/stancl/virtualcolumn/src/VirtualColumn.php:139 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1344 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php:1162 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:338 /vendor/laravel/framework/src/Illuminate/Collections/Traits/EnumeratesValues.php:257 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:333 /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Factories/Factory.php:287 /tests/Feature/Api/AuthenticateTest.php:27

stancl commented 1 month ago

For support questions please use our Discord.

SuperDJ commented 1 month ago

This appears to be caused by the events being fired too early. Using $tenant = Tenant::factory()->createQuietly() instead prevents the events from being fired.