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

Fix issue updating models with foreign key constraints #1988

Merged
merged 2 commits into from
Sep 9, 2018
Merged

Fix issue updating models with foreign key constraints #1988

merged 2 commits into from
Sep 9, 2018

Conversation

fillup
Copy link
Contributor

@fillup fillup commented Jul 16, 2018

Make sure these boxes checked before submitting your pull request.

  • Do only one thing
  • No API-breaking changes
  • New code/logic commented & tested

For significant changes like big bug fixes, new features, please open an issue to make an agreement on an implementation design/plan first before starting it.

What did this pull request do?

We found an issue where Gorm was trying to write the zero value of a has_one foreign key into the table and when using actual database foreign keys, the value 0 was invalid. This pull request adds a check in the updateCallback to ensure the field being update is not a blank foreign key with a default value. In our case the has_one relationship has a default value of null in the event that the model does not have the child yet.

All gorm tests pass and this makes it safe to use foreign keys with a default value of null.

@jinzhu jinzhu merged commit 588b598 into go-gorm:master Sep 9, 2018
@jinzhu
Copy link
Member

jinzhu commented Sep 9, 2018

thank you for your PR.

xuyang2 pushed a commit to easyops-cn/gorm that referenced this pull request Dec 11, 2018
* fix update callback to not try to write zero values when field has default value

* fix to update callback for gorm tests
blefevre pushed a commit to blefevre/gorm that referenced this pull request Feb 17, 2020
* fix update callback to not try to write zero values when field has default value

* fix to update callback for gorm tests
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