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: save with hook (#6285) #6294

Merged
merged 1 commit into from
May 26, 2023

Conversation

black-06
Copy link
Contributor

@black-06 black-06 commented May 5, 2023

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

What did this pull request do?

Hook is executed twice in Save func, close #6285

User Case Description

type Token struct {
	ID  int
	Content string `gorm:"type:varchar(100)"`
}

func (t *Token) BeforeSave(tx *gorm.DB) error {
	t.Content += "_encrypted"
	return nil
}

token := Token{Content: "token"}
DB.Save(&token) // "token_encrypted"

token = Token{
	ID:           token.ID,
	Content: "token"
}
DB.Save(&token) // mysql RowsAffected is 0, it should be "token_encrypted" instead of "token_encrypted_encrypted"

Copy link
Member

@a631807682 a631807682 left a comment

Choose a reason for hiding this comment

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

Does this mean that AfterSave has been executed during Update even if it is not updated the record (in this case AfterSave becomes BeforeSave)

@black-06
Copy link
Contributor Author

black-06 commented May 5, 2023

Does this mean that AfterSave has been executed during Update even if it is not updated the record (in this case AfterSave becomes BeforeSave)

Yes.

@a631807682
Copy link
Member

I seem to have other problems here, such as Create will not be executed when using Select, is Update necessary when we use OnConflict?
Of course, it's usually necessary to use Update and Create to avoid OnConflict, but if still need to use Create and OnConflict after Update, I'm not sure it's necessary.

@black-06
Copy link
Contributor Author

black-06 commented May 5, 2023

I seem to have other problems here, such as Create will not be executed when using Select, is Update necessary when we use OnConflict? Of course, it's usually necessary to use Update and Create to avoid OnConflict, but if still need to use Create and OnConflict after Update, I'm not sure it's necessary.

Yes, it is necessary. OnConflict is added at #6149 .

For example, token in database (MySQL):

id content
1 str
UPDATE `token` SET `content`='str' WHERE `id` = 1
-- rows: 0 (other db RowsAffected is 1)
-- Save will exec Create
-- Error 1062 (23000): Duplicate entry '1' for key 'token_owners.PRIMARY'

And this pr adds SkipHooks: true

@a631807682
Copy link
Member

I seem to have other problems here, such as Create will not be executed when using Select, is Update necessary when we use OnConflict? Of course, it's usually necessary to use Update and Create to avoid OnConflict, but if still need to use Create and OnConflict after Update, I'm not sure it's necessary.

Yes, it is necessary. OnConflict is added at #6149 .

For example, token in database (MySQL):

id content
1 str

UPDATE `token` SET `content`='str' WHERE `id` = 1
-- rows: 0 (other db RowsAffected is 1)
-- Save will exec Create
-- Error 1062 (23000): Duplicate entry '1' for key 'token_owners.PRIMARY'

And this pr adds SkipHooks: true

My question is not whether OnConflict is necessary, but whether Update is necessary

@black-06
Copy link
Contributor Author

black-06 commented May 5, 2023

I seem to have other problems here, such as Create will not be executed when using Select, is Update necessary when we use OnConflict? Of course, it's usually necessary to use Update and Create to avoid OnConflict, but if still need to use Create and OnConflict after Update, I'm not sure it's necessary.

Yes, it is necessary. OnConflict is added at #6149 .

For example, token in database (MySQL):

id content
1 str

UPDATE `token` SET `content`='str' WHERE `id` = 1
-- rows: 0 (other db RowsAffected is 1)
-- Save will exec Create
-- Error 1062 (23000): Duplicate entry '1' for key 'token_owners.PRIMARY'

And this pr adds SkipHooks: true

My question is not whether OnConflict is necessary, but whether Update is necessary

I have tried to create directly without update, but many tests have failed.
I guess it's related to perm and OnConflict will ignore them

@a631807682
Copy link
Member

I see, thank you.

@jinzhu jinzhu merged commit 11fdf46 into go-gorm:master May 26, 2023
24 checks passed
alidevhere pushed a commit to alidevhere/gorm that referenced this pull request May 30, 2023
jinzhu pushed a commit that referenced this pull request May 30, 2023
* max identifier length changed to 63

* default maxIdentifierLength is 64

* renamed License to LICENSE (#6336)

* Added support of "Violates Foreign Key Constraint" (#6329)

* Added support of "Violates Foreign Key Constraint"

Updated the translator and added the support of "foreign key constraint violation". For this, this error type is needed here.

* changed the description of ErrForeignKeyViolated

* refactor: error translator test (#6350)

Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com>

* fix(nested transaction): SavePoint SQL Statement not support in Prepared Statements (#6220)

* test: add nested transaction and prepareStmt coexist test case

note: please test in the MySQL environment

Change-Id: I0db32adc5f74b0d443e98943d3b182236583b959
Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com>

* fix(nested transaction): SavePoint SQL Statement not support in Prepared Statements

1. SavetPoint SQL Statement not support in Prepared Statements
 e.g. see mysql8.0 doc: https://dev.mysql.com/doc/refman/8.0/en/sql-prepared-statements.html

Change-Id: I082012db9b140e8ec69764c633724665cc802692
Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com>

* revert(transaction_api): remove savepoint name pool,meaningless

Change-Id: I84aa9924fc54612005a81c83d66fdf8968ee56ad
Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com>

---------

Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com>
Co-authored-by: 王柳洋 <wangliuyang.520@bytedance.com>

* fix: save with hook (#6285) (#6294)

---------

Signed-off-by: 王柳洋 <wangliuyang.520@bytedance.com>
Co-authored-by: Avinaba Bhattacharjee <avinababhattacharjee2002@gmail.com>
Co-authored-by: Muhammad Amir Ejaz <37077032+codingamir@users.noreply.github.com>
Co-authored-by: Saeid <sk.saeidee@yahoo.com>
Co-authored-by: Saeid Saeidee <s.saeidee@sensysgatso.com>
Co-authored-by: wangliuyang <54885906+wangliuyang520@users.noreply.github.com>
Co-authored-by: 王柳洋 <wangliuyang.520@bytedance.com>
Co-authored-by: black-06 <hello.bug@foxmail.com>
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.

BeforeSave is executed twice in transaction
3 participants