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

feat: unscoped association (#5899) #6246

Merged
merged 6 commits into from
May 4, 2023

Conversation

black-06
Copy link
Contributor

  • Do only one thing
  • Non breaking API changes
  • Tested

What did this pull request do?

delete old records when Replace, Delete, Clear associations in Unscoped model.
Of course, permanent or soft deletion depends on DB.Unscoped.

See #5899

User Case Description

// Remove all reference between source & association, won’t delete those associations, set ForeignKey to nil
tx.Model(&item).Association("Contents").Clear()

// Soft delete
// UPDATE `item_contents` SET `deleted_at`= ...
tx.Model(&item).Association("Contents").Unscoped().Clear()

// Delete permanently
// DELETE FROM `item_contents` WHERE ...
tx.Unscoped().Model(&item).Association("Contents").Unscoped().Clear()

@black-06
Copy link
Contributor Author

Did I do something wrong? The test failed.

SQLite: /home/runner/work/gorm/gorm/tests/associations_belongs_to_test.go:140 FOREIGN KEY constraint failed

MySQL: /home/runner/work/gorm/gorm/tests/associations_belongs_to_test.go:140 Error 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`gorm`.`users`, CONSTRAINT `fk_users_company` FOREIGN KEY (`company_id`) REFERENCES `companies` (`id`))

@a631807682
Copy link
Member

a631807682 commented Apr 19, 2023

I searched by --- FAIL keyword, did not see the error you said in CI.
sqlite related to go-gorm/sqlite#133

--- FAIL: TestSerializerZeroValue (0.01s)
    serializer_test.go:143: failed to query data, got error unsupported Scan, storing driver.Value type string into type *time.Time

mysql seems to be related to this PR, or an existing bug has been tested. If it is confirmed that it has nothing to do with PR, you can notify me to review.

--- FAIL: TestAssociationUnscoped (0.02s)
    associations_test.go:423: failed to create items, got error: Error 13[66](https://github.com/go-gorm/gorm/actions/runs/4731373972/jobs/8396215932?pr=6246#step:6:67) (HY000): Incorrect string value: '\xD8\xA7\xD9\x84\xD8\xA7...' for column 'name' at row 2

@black-06
Copy link
Contributor Author

I searched by --- FAIL keyword, did not see the error you said in CI. sqlite related to go-gorm/sqlite#133

--- FAIL: TestSerializerZeroValue (0.01s)
    serializer_test.go:143: failed to query data, got error unsupported Scan, storing driver.Value type string into type *time.Time

mysql seems to be related to this PR, or an existing bug has been tested.

--- FAIL: TestAssociationUnscoped (0.02s)
    associations_test.go:423: failed to create items, got error: Error 13[66](https://github.com/go-gorm/gorm/actions/runs/4731373972/jobs/8396215932?pr=6246#step:6:67) (HY000): Incorrect string value: '\xD8\xA7\xD9\x84\xD8\xA7...' for column 'name' at row 2

Got it.

@black-06
Copy link
Contributor Author

black-06 commented Apr 19, 2023

Before this pr, we need to merge #6250 first (for SQLite test)

And #6127 (comment) (for MySQL test)
(Do I modify it directly in #6250? Or another pr?)

@black-06
Copy link
Contributor Author

By the way, how should I notify you @a631807682 .

Assignees
No one assigned

I don't use GitHub very well.

@a631807682
Copy link
Member

a631807682 commented Apr 19, 2023

Both ok, because it's all about fixing unit tests.
Comment or update the code on an issue or PR I've commented on and I'll be notified, or just @ me

association.go Outdated Show resolved Hide resolved
@black-06 black-06 requested a review from a631807682 April 23, 2023 09:26
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.

3 participants