pingcap / pingcap/tidb

gorm: modify column with unique attribute return error

Open
#34,535 1 comment 0 reactions 0 assignees View on GitHub
compatibility-mysql80 sig/sql-infra type/feature-request
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

#### Reproduce by gorm

```go
package main

import (
"gorm.io/driver/mysql"
"gorm.io/gorm"
)

type User struct {
gorm.Model
Name string
Code string `gorm:"unique;comment:my code;"`
}

type User2 struct {
gorm.Model
Name string `gorm:"size:100"`
Code string `gorm:"unique;comment:my code2;default:hello"`
}

func main() {
dsn := "root:@tcp(127.0.0.1:4000)/test?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
mustBeNil(err)

db.Migrator().DropTable(&User{})
err = db.AutoMigrate(&User{})
mustBeNil(err)

err = db.Table("users").AutoMigrate(&User2{})
mustBeNil(err)
}

func mustBeNil(err error) {
if err != nil {
panic(err)
}
}
```

#### Reproduce by SQL

```sql
CREATE TABLE `users` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
`code` varchar(191) DEFAULT NULL COMMENT 'my code',
PRIMARY KEY (`id`),
UNIQUE KEY `code` (`code`)
);
ALTER TABLE `users` MODIFY COLUMN `code` varchar(191) UNIQUE DEFAULT 'hello' COMMENT 'my code2';
```

### 2. What did you expect to see? (Required)

MySQL will execute success, I think TiDB should also execute success.

### 3. What did you see instead (Required)

```sql
>ALTER TABLE `column_structs` MODIFY COLUMN `code` varchar(191) UNIQUE DEFAULT 'hello' COMMENT 'my code2';
(8200, "can't change column constraint - 5")
```

### 4. What is your TiDB version? (Required)

```sql
>select tidb_version()\G
***************************[ 1. row ]***************************
tidb_version() | Release Version: v6.1.0-alpha-360-gab26a287dc
Edition: Community
Git Commit Hash: ab26a287dcbe4186d327cfcaa74bf945db83a338
Git Branch: master
UTC Build Time: 2022-05-10 08:32:31
GoVersion: go1.18
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
1 row in set

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.