Skip to content

Added support of "Violates Foreign Key Constraint"#6329

Merged
jinzhu merged 2 commits intogo-gorm:masterfrom
amirejaz75:patch-1
May 21, 2023
Merged

Added support of "Violates Foreign Key Constraint"#6329
jinzhu merged 2 commits intogo-gorm:masterfrom
amirejaz75:patch-1

Conversation

@amirejaz75
Copy link
Copy Markdown
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.
@amirejaz75
Copy link
Copy Markdown
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
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 <[email protected]>

* 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: 王柳洋 <[email protected]>

* 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: 王柳洋 <[email protected]>

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

Change-Id: I84aa9924fc54612005a81c83d66fdf8968ee56ad
Signed-off-by: 王柳洋 <[email protected]>

---------

Signed-off-by: 王柳洋 <[email protected]>
Co-authored-by: 王柳洋 <[email protected]>

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

---------

Signed-off-by: 王柳洋 <[email protected]>
Co-authored-by: Avinaba Bhattacharjee <[email protected]>
Co-authored-by: Muhammad Amir Ejaz <[email protected]>
Co-authored-by: Saeid <[email protected]>
Co-authored-by: Saeid Saeidee <[email protected]>
Co-authored-by: wangliuyang <[email protected]>
Co-authored-by: 王柳洋 <[email protected]>
Co-authored-by: black-06 <[email protected]>
@amirejaz75
Copy link
Copy Markdown
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.

3 participants