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

Update from #6579

Closed
wants to merge 1 commit into from
Closed

Update from #6579

wants to merge 1 commit into from

Conversation

FrancoLiberali
Copy link
Contributor

@FrancoLiberali FrancoLiberali commented Sep 6, 2023

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

What did this pull request do?

To demonstrate that the changes in the following pull requests make sense:
go-gorm/sqlserver#115, go-gorm/sqlite#166, go-gorm/postgres#213

Add a test to show that "update from" is possible without making changes to the code (by adding the clause.From manually).

This test will not work until the new versions of the dialectors are used. I don't know what flow you follow to make this update.

User Case Description

Add an EXISTS condition to an UPDATE in a simpler way:

UPDATE table_name_1
SET attribute = "value"
FROM table_name_2
WHERE table_name_2.table_1_id = table_name_1.id

This is equivalent to:

UPDATE table_name_1
SET attribute = "value"
WHERE EXISTS (select 1 FROM table_name_2 WHERE table_name_2.table_1_id = table_name_1.id)

@FrancoLiberali FrancoLiberali force-pushed the update_from branch 2 times, most recently from b72b004 to 547a294 Compare September 7, 2023 14:33
@jinzhu jinzhu marked this pull request as ready for review October 10, 2023 07:10
@@ -882,3 +882,52 @@ func TestSaveWithHooks(t *testing.T) {
t.Errorf(`token content should be "token2_encrypted", but got: "%s"`, o2.Token.Content)
}
}

// only postgres, sqlserver, sqlite support update from
func TestUpdateFrom(t *testing.T) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the test failed when running all test cases

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jinzhu It seems that the problem is not these tests, but the new versions of the drivers that were taged and are used to run the tests in the ci (in master the tests passed because the ci was run before the new tags were created). For the postgree driver the v1.5.3 tag was created but the tests do not pass using this version (they do not pass because of the changes in the commit go-gorm/postgres@7ba909e specifically). In the same way, in the mysql driver the v1.5.2 tag was created but the tests don't pass since the changes of the commit go-gorm/mysql@2a61ba0. In my fork (https://github.com/ditrit/gorm) you can see the master tests do not pass with the new driver tags

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@FrancoLiberali The test failure has nothing to do with this PR. You seem to have investigated the cause of the failure. Are you interested in creating other PRs to fix it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@a631807682 To be honest, I only ran the tests on the different driver's commits to find out from which one the tests don't work, but I don't know what is the purpose of the changes in those commits to fix the generated problems

@jinzhu
Copy link
Member

jinzhu commented Dec 4, 2023

merged by f0af94c

@jinzhu jinzhu closed this Dec 4, 2023
@FrancoLiberali FrancoLiberali deleted the update_from branch December 18, 2023 22:19
@haihanj
Copy link

haihanj commented Jan 13, 2024

Is it possible you can write some sample code how to translate this SQL sentence into GORM?

@FrancoLiberali
Copy link
Contributor Author

@haihanj As you can see in the added tests, the way is to add the From clause manually:

DB.Model(&User{}).Clauses(clause.From{Tables: []clause.Table{{Name: "accounts"}}}).Where("accounts.user_id = users.id AND accounts.number = ? AND accounts.deleted_at IS NULL", users[0].Account.Number).Update("name", "franco")

I hope this is helpful

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.

4 participants