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

Unexpected behaviour creating through HasOne #51567

Closed
PerryRylance opened this issue May 25, 2024 · 6 comments
Closed

Unexpected behaviour creating through HasOne #51567

PerryRylance opened this issue May 25, 2024 · 6 comments

Comments

@PerryRylance
Copy link

Laravel Version

11.0

PHP Version

8.3

Database Driver & Version

Postgres 15 on Ubuntu via WSL2

Description

I have the following scenario:

  • A Cup model
  • A Teabag model
  • A cup migration, it has a nullable foreign ID teabag_id

In other words, the cup may or may not contain a teabag.

I've provided a test that creates a Cup then attempts to use the relationship to create a Teabag through the Cup.

Steps To Reproduce

  • Create project via composer
  • Create cup model with migration including teabag_id as foreign ID for teabag, nullable
  • Create teabag model with migration
  • Create a test that creates a cup, then tries to create a teabag via $cup->teabag()->create
  • Install sail
  • Sail up
  • Run sail test

Expected:

Either a meaningful error message (eg stating that this is not supported) or for the teabag to be created through cup.

Observed:

Laravel tries to set id as null on the teabag which causes Postgres to fail on a non-null constraint.

Notes:

Not too sure why it's trying to set an ID there. Behaviour is different on MariaDB, the DB doesn't fail, but the relationship is null after calling create.

Please see my MRE here

@crynobone
Copy link
Member

https://github.com/PerryRylance/pgsql-has-one-null-id/commit/0edd95fab44766a88d5e31844827119b7b7e5b0a

It's very hard to follow the repository without a separate commit focusing on the issue.

@crynobone
Copy link
Member

Hey there, thanks for reporting this issue.

We'll need more info and/or code to debug this further. Can you please create a repository with the command below, commit the code that reproduces the issue as one separate commit on the main/master branch and share the repository here?

Please make sure that you have the latest version of the Laravel installer in order to run this command. Please also make sure you have both Git & the GitHub CLI tool properly set up.

laravel new bug-report --github="--public"

Do not amend and create a separate commit with your custom changes. After you've posted the repository, we'll try to reproduce the issue.

Thanks!

@PerryRylance
Copy link
Author

This repo is focused solely on this issue, if that helps. I will recreate it using the command there, thank you.

@driesvints
Copy link
Member

driesvints commented May 27, 2024

You got this wrong. As the docs state, you need to define the foreign key on the model that's related to the model calling the hasOne method. So teabags need to have the foreign key to your cups table.

@PerryRylance
Copy link
Author

I did indeed get this one wrong, thank you.

I ended up using belongsTo on the cup instead so that I can keep my keys where they need to be, so that the cup may have zero or one teabags.

Sorry for the fuss folks! Hopefully this helps someone.

@driesvints
Copy link
Member

@PerryRylance no worries at all 👍

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