Skip to content

fix: The HasConstraint is fixed by adding a Unique Check#173

Merged
jinzhu merged 3 commits intogo-gorm:masterfrom
Orefa:master
Jan 31, 2026
Merged

fix: The HasConstraint is fixed by adding a Unique Check#173
jinzhu merged 3 commits intogo-gorm:masterfrom
Orefa:master

Conversation

@Orefa
Copy link
Contributor

@Orefa Orefa commented Nov 19, 2025

add Unique Check

@jinzhu
Copy link
Member

jinzhu commented Nov 22, 2025

Hi @Orefa can you add some tests for the changes?

@Orefa
Copy link
Contributor Author

Orefa commented Nov 24, 2025

Hi @Orefa can you add some tests for the changes?
已经添加了,目前看到gorm当中源码,并不支持多字段uk,如果通过sql添加了多字段uk后, 再次执行AutoMigrate 会报错,fix: Composite unique constraint 是参考postgres的实现进行的修改

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the SQL Server migrator so HasConstraint can detect UNIQUE constraints, and adds a regression test to cover the behavior.

Changes:

  • Extend Migrator.HasConstraint to also query SQL Server’s unique constraints (sys.key_constraints type UQ).
  • Refine Migrator.ColumnTypes UNIQUE detection so only single-column UNIQUE constraints mark a column as Unique().
  • Add a new test table + test case to validate HasConstraint behavior for a UNIQUE column.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.

File Description
migrator.go Adds UNIQUE constraint detection to HasConstraint and adjusts ColumnTypes UNIQUE flag inference.
migrator_test.go Adds a regression test verifying HasConstraint returns true for a UNIQUE field.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +414 to +418
for columnTypeRows.Next() {
var constraintName string
columnTypeRows.Scan(&constraintName)
uniqueContraints[constraintName]++
}
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

Row scanning errors are ignored when building the unique-constraint cardinality map (columnTypeRows.Scan(&constraintName)). If Scan fails, the map can be incomplete and later UniqueValue inference becomes wrong. Please handle the Scan error (and consider checking columnTypeRows.Err() after the loop) and return the error.

Copilot uses AI. Check for mistakes.
Comment on lines 435 to +447
for columnTypeRows.Next() {
var name, columnType string
_ = columnTypeRows.Scan(&name, &columnType)
var name, constraintName, columnType string
_ = columnTypeRows.Scan(&name, &constraintName, &columnType)
for idx, c := range columnTypes {
mc := c.(migrator.ColumnType)
if mc.NameValue.String == name {
switch columnType {
case "PRIMARY KEY":
mc.PrimaryKeyValue = sql.NullBool{Bool: true, Valid: true}
case "UNIQUE":
mc.UniqueValue = sql.NullBool{Bool: true, Valid: true}
if uniqueContraints[constraintName] == 1 {
mc.UniqueValue = sql.NullBool{Bool: true, Valid: true}
}
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

Row scanning errors are ignored when reading constraint metadata (_ = columnTypeRows.Scan(&name, &constraintName, &columnType)). If the scan fails, the loop will silently skip setting PrimaryKey/Unique flags. Please handle Scan errors (and optionally Rows.Err() after iteration) so ColumnTypes returns a deterministic error instead of silently degrading.

Copilot uses AI. Check for mistakes.
Comment on lines +400 to +408
query := "SELECT t.CONSTRAINT_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS t JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE c ON c.CONSTRAINT_NAME=t.CONSTRAINT_NAME WHERE t.CONSTRAINT_TYPE IN ('PRIMARY KEY', 'UNIQUE') AND c.TABLE_CATALOG = ? AND c.TABLE_NAME = ? AND CONSTRAINT_TYPE = ?"

queryParameters := []interface{}{m.CurrentDatabase(), tableName}

if schemaName != "" {
query += " AND c.TABLE_SCHEMA = ?"
queryParameters = append(queryParameters, schemaName)
}

queryParameters = append(queryParameters, "UNIQUE")
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

The UNIQUE-constraint prequery is redundant and slightly unclear: it filters t.CONSTRAINT_TYPE IN ('PRIMARY KEY','UNIQUE') and also adds AND CONSTRAINT_TYPE = ? (unqualified). Consider simplifying to a single, qualified predicate (e.g., t.CONSTRAINT_TYPE = 'UNIQUE') to avoid confusion and keep the query planner work minimal.

Copilot uses AI. Check for mistakes.
Comment on lines +413 to +417
uniqueContraints := map[string]int{}
for columnTypeRows.Next() {
var constraintName string
columnTypeRows.Scan(&constraintName)
uniqueContraints[constraintName]++
Copy link

Copilot AI Jan 31, 2026

Choose a reason for hiding this comment

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

Typo in variable name uniqueContraints; this reduces readability and makes future refactors/searching harder. Rename to uniqueConstraints.

Copilot uses AI. Check for mistakes.
@jinzhu jinzhu merged commit fc75edd into go-gorm:master Jan 31, 2026
9 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.

3 participants