Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Fix foreignIdFor() when the foreign key is a non-incrementing integer other than ULID #53696

Merged

Conversation

edgrosvenor
Copy link
Contributor

This issue has been raised before: #46211

When using a non-incrementing integer as a primary key and then referencing that key using foreignIdFor() the resulting column is a UUID, which is the default return on the method. We caught this because we use snowflakes as IDs in an app we're working on and when we moved from sqlite to postgres, we started getting errors (sqlite is too forgiving sometimes).

If I were doing this on the 12.x branch, I might be inclined to use a check similar to the HasUlids::class check for HasUuids::class and make a non-incrementing big integer the default instead. I think most people who are using snowflakes as IDs are using https://github.com/glhd/bits which offers a HasSnowflakes trait similar to the ones provided for UUIDs and ULIDs in Laravel, but since it's a 3rd party package, checking for that trait here doesn't make sense. But it feels like changing the default return value is too big a change to make outside of a major release, even though it is extremely unlikely that it would cause anyone any problems but might correct an unseen error for people using SQLite.

I've simply added another check under the ULID check for non-incrementing integers and left the rest of the method as-is. If a more aggressive fix is desirable either on this branch or on 12.x I'd be happy to take it on.

@edgrosvenor edgrosvenor changed the title Fix foreignIdFor() when the foreign key is a on-incrementing integer other than ULID [11.x] Fix foreignIdFor() when the foreign key is a on-incrementing integer other than ULID Nov 28, 2024
@edgrosvenor edgrosvenor changed the title [11.x] Fix foreignIdFor() when the foreign key is a on-incrementing integer other than ULID [11.x] Fix foreignIdFor() when the foreign key is a non-incrementing integer other than ULID Nov 28, 2024
@@ -405,6 +405,22 @@ public function testGenerateRelationshipColumnWithIncrementalModel()
], $blueprint->toSql($connection, new MySqlGrammar));
}

public function testGenerateRelationshipColumnWithNonIncrementalModel()
{
require_once __DIR__.'/stubs/EloquentModelNonIncrementedIntStub.php';
Copy link
Member

Choose a reason for hiding this comment

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

Please follow existing autoloading structure when importing stub class

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'd be happy to, but I'm not totally sure what you mean. Visually, this looks exactly like what you did in testGenerateRelationshipColumnWithUuidModel(), so a little guidance would be greatly appreciated.

Copy link
Contributor

@ProjektGopher ProjektGopher Nov 28, 2024

Choose a reason for hiding this comment

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

As far as I can tell, this is the way stubs are already being imported in this test.

See lines 426, 443, 513, 546...

Copy link
Member

Choose a reason for hiding this comment

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

Fixed, stubs are for mocking and fixtures are test code/data used in tests

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I was just going by what the code surrounding my work was doing. I haven't been paying close attention to how coding standards have evolved there. This is actually much more intuitive!

Signed-off-by: Mior Muhammad Zaki <[email protected]>
@crynobone crynobone self-requested a review November 28, 2024 11:36
@crynobone crynobone self-requested a review November 29, 2024 14:09
@taylorotwell taylorotwell merged commit bbd61dd into laravel:11.x Nov 29, 2024
38 checks passed
@edgrosvenor edgrosvenor deleted the foreign_id_for_non_incrementing branch December 1, 2024 09:24
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.

4 participants