AutoMigrate
Does Not Update Existing Check Constraint
#7212
Labels
type:with reproduction steps
with reproduction steps
GORM Playground Link
go-gorm/playground#760
Description
Summary:
When modifying a check constraint on a model field and running
AutoMigrate
, GORM does not update the existing check constraint in the database if a constraint with the same name already exists. This issue is observed with SQLite and may affect other database dialects as well.Steps to Reproduce:
Set up the initial model with a check constraint and run
AutoMigrate
:Modify the check constraint in the model and run
AutoMigrate
again:Attempt to insert data that violates the new constraint but satisfies the old one:
Observe the Output:
This indicates that the check constraint was not updated in the database after modifying the model and running
AutoMigrate
again.Expected Behavior:
AutoMigrate
, the existing constraint in the database should be updated to reflect the changes in the model.Actual Behavior:
Possible Cause:
AutoMigrate
function checks for the existence of constraints by name and skips creation if the constraint exists, without verifying if the constraint definition has changed.Environment:
v1.25.12
go 1.23.0
gorm.io/driver/sqlite
but I was able to reproduce the same problem in postgres (I didn't try with other dialects but imagine the same issue exists)Additional Context:
Workaround:
As a temporary workaround, manually drop the existing constraint before running
AutoMigrate
:This forces the constraint to be recreated with the updated definition.
Possible Solution:
AutoMigrate
logic to drop existing constraints with the same name before creating new ones.Note: I apologies for the duplicate issue submissions. I was figuring out how to do the Playground Pull Request.
The text was updated successfully, but these errors were encountered: