stancl/tenancy-docs

If tenancy.id_generator is null in tenancy config, resource syncing is not working.

nopedev opened this issue · 0 comments

Thank you for great package.

In the Tenants document says

If you wish to use numerical ids, change the create_tenants_table migration to use bigIncrements() 
or some such column type, and set tenancy.id_generator config to null. That will disable 
the ID generation altogether, falling back to the database's autoincrement mechanism.

and I wanted to use numeric tenant id so I followed the instruction(changing migration and disabling ID generator in config) and it worked well.

Next I wished to use resource syncing feature, so followed the sample codes. but when I tried to create new user in tenant route it failed with SQL error.

SQLSTATE[HY000]: General error: 1364 Field 'global_id' doesn't have a default value
...

After some debug, I found below codes at ResourceSyncing trait.

static::creating(function (self $model) {
  if (! $model->getAttribute($model->getGlobalIdentifierKeyName()) && app()->bound(UniqueIdentifierGenerator::class)) {
    $model->setAttribute(
      $model->getGlobalIdentifierKeyName(),
      app(UniqueIdentifierGenerator::class)->generate($model)
    );
   }
 });

It checks app()->bound(UniqueIdentifierGenerator::class) so I enabled ID generator in config and it worked again.

I think it will be good if there is some note in doc about resource syncing feature requires ID generator.