Skip to content

Conversation

@nowindexman
Copy link
Contributor

bugfix:AutoMigrate #7590

@propel-code-bot
Copy link
Contributor

propel-code-bot bot commented Sep 18, 2025

Fix AutoMigrate Default Value Comparison for String Fields

This pull request addresses issue #7590 by refining how AutoMigrate determines whether a column should be altered when comparing string-type default values. The update introduces special logic for string fields to handle discrepancies caused by surrounding quotes, ensuring that unnecessary column alterations are avoided if only quotation differences exist. The solution introduces a check that leverages strings.Trim(field.DefaultValue, "'\"") for more accurate comparison, following reviewer feedback.

Key Changes

• Added a case schema.String branch to the default value comparison logic in migrator/migrator.go.
• Comparison now ensures that a column is only flagged for alteration if the existing default value differs from both the raw and trimmed (quotes removed) forms of the schema default.
• Utilized strings.Trim(field.DefaultValue, "'\"") to eliminate discrepancies due solely to surrounding quotes.
• No other functionality outside default value comparison is affected.

Affected Areas

migrator/migrator.go - logic within the MigrateColumn function as part of the type/default value comparison flow

This summary was automatically generated by @propel-code-bot

Comment on lines 563 to 566
case schema.String:
if dv != field.DefaultValue || dv != strings.Trim(field.DefaultValue, "'") {
alterColumn = true
}
Copy link
Contributor

Choose a reason for hiding this comment

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

[CriticalError]

Logic error in string default value comparison: The condition dv != field.DefaultValue || dv != strings.Trim(field.DefaultValue, "'") will always evaluate to true because of the OR operator. If dv == field.DefaultValue, then dv != strings.Trim(field.DefaultValue, "'") will likely be true (unless the field value has quotes). This means alterColumn will always be set to true for string fields, causing unnecessary column alterations.

The logic should use AND operator to check if the values are different in both forms:

Suggested change
case schema.String:
if dv != field.DefaultValue || dv != strings.Trim(field.DefaultValue, "'") {
alterColumn = true
}
case schema.String:
if dv != field.DefaultValue && dv != strings.Trim(field.DefaultValue, "'") {
alterColumn = true
}

Committable suggestion

Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Context for Agents
[**CriticalError**]

Logic error in string default value comparison: The condition `dv != field.DefaultValue || dv != strings.Trim(field.DefaultValue, "'")` will always evaluate to `true` because of the OR operator. If `dv == field.DefaultValue`, then `dv != strings.Trim(field.DefaultValue, "'")` will likely be true (unless the field value has quotes). This means `alterColumn` will always be set to `true` for string fields, causing unnecessary column alterations.

The logic should use AND operator to check if the values are different in both forms:

```suggestion
			case schema.String:
				if dv != field.DefaultValue && dv != strings.Trim(field.DefaultValue, "'") {
					alterColumn = true
				}
```

⚡ **Committable suggestion**

Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

File: migrator/migrator.go
Line: 566

@propel-code-bot propel-code-bot bot changed the title bugfix:AutoMigrate https://github.com/go-gorm/gorm/issues/7590 Fix AutoMigrate default value comparison for string fields (issue #7590) Sep 18, 2025
v2, _ := strconv.ParseBool(field.DefaultValue)
alterColumn = v1 != v2
case schema.String:
if dv != field.DefaultValue && dv != strings.Trim(field.DefaultValue, "'") {
Copy link
Member

Choose a reason for hiding this comment

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

change to strings.Trim(field.DefaultValue, "'\"")?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it, already updated.

@nowindexman
Copy link
Contributor Author

@jinzhu Can this be merged into the main branch?

@jinzhu jinzhu merged commit 141388f into go-gorm:master Oct 26, 2025
25 checks passed
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.

2 participants