Skip to content

Conversation

@imhayatunnabi
Copy link
Contributor

Github Issue: #2888
The team_foreign_key column was nullable in the roles table but not in model_has_permissions & model_has_roles pivot tables. This caused errors when assigning global roles (with team_foreign_key = null) to users, as the pivot tables rejected NULL values.

Verified that migration files already correctly set `team_foreign_key` as nullable in both pivot tables:
- `add_teams_fields.php.stub` (lines 43 & 64)
- `create_permission_tables.php.stub` (lines 65 & 92)

Added test coverage to verify the fix and prevent regressions:

  • it_can_assign_global_role_with_null_team_foreign_key_to_user() - Core scenario from issue
  • it_can_assign_global_role_to_multiple_users_across_different_teams() - Unique constraint with NULL
  • it_can_query_global_roles_correctly_across_different_teams() - Query logic with NULL
  • it_allows_direct_database_insertion_with_null_team_foreign_key() - Direct schema test
  • it_handles_mixed_team_and_global_roles_correctly() - Edge cases
  • it_allows_direct_database_insertion_with_null_team_foreign_key_for_permissions() - Permissions table

…s_permissions and model_has_roles tables and relevent tests to proof
Copy link

@runofthemill runofthemill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @imhayatunnabi for this fix! I ran into an issue, noted a possible fix below

$table->primary([$pivotPermission, $columnNames['model_morph_key'], 'model_type'],
'model_has_permissions_permission_model_type_primary');
$table->unique([$columnNames['team_foreign_key'], $pivotPermission, $columnNames['model_morph_key'], 'model_type'],
'model_has_permissions_team_foreign_key_permission_model_type_unique');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noting this identifier is too long for MySQL; char limit is 64, and this is 67; possible fix:

Suggested change
'model_has_permissions_team_foreign_key_permission_model_type_unique');
'model_has_permissions_team_fk_permission_model_type_unique');

$table->primary([$columnNames['team_foreign_key'], $pivotPermission, $columnNames['model_morph_key'], 'model_type'],
'model_has_permissions_permission_model_type_primary');
$table->unique([$columnNames['team_foreign_key'], $pivotPermission, $columnNames['model_morph_key'], 'model_type'],
'model_has_permissions_team_foreign_key_permission_model_type_unique');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above;

Suggested change
'model_has_permissions_team_foreign_key_permission_model_type_unique');
'model_has_permissions_team_fk_permission_model_type_unique');

@imhayatunnabi
Copy link
Contributor Author

@runofthemill the suggestion and reviews has been updated. can you please check again ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants