[12.x] Feature/support/bugfix foreign string #55466
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ForeignIdFor issue
Hi, when changing Models
$keyTypetostring,foreignIdFor()method does not work as expected.Example/Reproduction
Models
Migrations
Tinker
Env setup
Setup laravel app with sail and choose pgsql. I tested this issue only for pgsql and mysql (I did not test maria, sqlite, etc.). Mysql works fine but pgsql does not work because it expects UUID (see the code) and not a custom string.
(Braking) changes
Model will have to
use HasUuids;if it uses UUID as its primary key when specifying a foreign id using foreignIdFor() methodforeignStringmethod - I am not sure if it should be extracted or the code inside the function should be embedded intoforeignIdFormethod.Current workaround
when building migrations use foreign()
I had to also reorder (rename timestamps) of migration files so that previously created migration does not depend on future migrations. When using
foreignIdFor, the order of running migrations does not matter.Other possible workaround is using mysql (since it works there) but DB migration is complicated.
Future work
If this or similar change gets accepted I will update Laravel docs. There is the same problem with morph relations, i could not use
morphmethod and instead had to specify custom keys in migrations.Feel free to contribute to PR and ask any questions, I will open an issue for this bug/feature if needed.