Skip to content

When migrating MySQL database tables, if the new table adds a primary key, the migration will fail #6490

@usernameisnull

Description

@usernameisnull

GORM Playground Link

go-gorm/playground#1

Description

old table:

CREATE TABLE `workspace_binding` (
  `toolchain_id` int NOT NULL,
  `workspace_id` int NOT NULL,
  `resource_id` int NOT NULL,
  UNIQUE KEY `tid_wid_rid_unique_index` (`toolchain_id`,`workspace_id`,`resource_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3

new talbe:

CREATE TABLE `workspace_binding_ddd` (
  `id` bigint NOT NULL AUTO_INCREMENT,
  `created_at` datetime(3) DEFAULT NULL,
  `updated_at` datetime(3) DEFAULT NULL,
  `toolchain_id` bigint NOT NULL,
  `workspace_id` bigint NOT NULL,
  `resource_id` bigint NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `tid_wid_rid_unique_index` (`toolchain_id`,`workspace_id`,`resource_id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb3

when auto migrate, the error is:

ALTER TABLE `workspace_binding` ADD `id` bigint AUTO_INCREMENT
Incorrect table definition; there can be only one auto column and it must be defined as a key

The statement should be: ALTER TABLE workspace_bindingADDid bigint AUTO_INCREMENT primary key, the primary key missed
is there any solution?

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions