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

Added support of "Violates Foreign Key Constraint" #6329

Merged
merged 2 commits into from
May 21, 2023

Conversation

amirejaz75
Copy link
Contributor

@amirejaz75 amirejaz75 commented May 18, 2023

  • [Y ] Do only one thing
  • [ Y] Non breaking API changes
  • [ Y] Tested

What did this pull request do?

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

User Case Description

Lets say I have a struct RoleAssignment having RoleID as a foreign key

type RoleAssignment struct {
	RoleID      int         `gorm:"type:INTEGER;NOT NULL"`
	Locations   string      `gorm:"type:VARCHAR(255);NOT NULL"`
	SubjectID   int         `gorm:"type:INTEGER;NOT NULL"`
	SubjectType SubjectType `gorm:"type:VARCHAR(255);NOT NULL"`
	Role        Role
}

and I want to insert a record having role_id which does not exist in Role table, here gorm is throwing pgerror which should be translated to gorm error. As suggested below:

result := tx.Create(&roleAssignment)
		if result.Error != nil {
			if errors.Is(result.Error, gorm.ErrForeignKeyViolated) {
				// return Custom Error from here 
			}
		}

Pull Request for https://github.com/go-gorm created.

Updated the translator and added the support of "foreign key constraint violation". For this, this error type is needed here.
errors.go Outdated Show resolved Hide resolved
@amirejaz75
Copy link
Contributor Author

amirejaz75 commented May 19, 2023

@jinzhu kindly merge this PR as it's needed for go-gorm/postgres#185 (already merged). Thanks.

@jinzhu jinzhu merged commit 001738b into go-gorm:master May 21, 2023
23 checks passed
alidevhere pushed a commit to alidevhere/gorm that referenced this pull request May 30, 2023
* 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
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>
@amirejaz75
Copy link
Contributor Author

@jinzhu please release new changes as these are needed in gorm/postgres. Thank you.

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.

None yet

3 participants