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

Morph relation breaks when using different key names #36644

Closed
axlon opened this issue Mar 18, 2021 · 2 comments
Closed

Morph relation breaks when using different key names #36644

axlon opened this issue Mar 18, 2021 · 2 comments

Comments

@axlon
Copy link
Contributor

axlon commented Mar 18, 2021

  • Laravel Version: v8.33.0 and v8.33.1 (I tested on these, but the bug was introduced in v8.29.0)
  • PHP Version: 7.4.14
  • Database Driver & Version: n/a

Description:

Recently a PR (#36303) was merged which changes the behavior of the morphTo relationship, this relationship however now malfunctions when using custom model primary keys. When you associate 2 models with different key names to a morphTo relation, the framework will set the morph key to null. (The different models don't have to be associated in the same request lifecycle, as long as the old relation is loaded when the new one is associated this seems to happen)

Steps To Reproduce:

(I created a temporary repository which shows the full problem, I changed the default route in routes/web.php to show the bug)

  • Create a model (Let's call this model A)
  • Create another model with a different key name (Let's call this model B)
  • Create a third model with a morphTo relationship (Model C)
  • Use the associate method to attach an instance of A to C, if you check C's attributes you will see both morph type and key are set (everything's fine so far)
  • Use the associate method to attach an instance of B to that same instance of C, if you check C's attributes you will see that the morph type is set, but the morph key is null
@driesvints
Copy link
Member

Ping @garbetjie

@taylorotwell
Copy link
Member

Hey @axlon - I don't think we are going to revert that as it just introduces another bug. My suggestion is to disassociate the first association before connecting another one of a different type when using custom primary keys.

image

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

No branches or pull requests

3 participants