Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

复合主键没有起作用 #3514

Closed
lbfatcgf opened this issue Sep 23, 2020 · 1 comment
Closed

复合主键没有起作用 #3514

lbfatcgf opened this issue Sep 23, 2020 · 1 comment
Assignees
Labels

Comments

@lbfatcgf
Copy link

lbfatcgf commented Sep 23, 2020

GORM Playground Link

xx

Description

迁移数据库时没有生成复合主键。
我预期的sql是CREATE TABLE member (room_id bigint,user_id bigint,status bigint) PRIMARY KEY (room_id,user_id)
,但是info的结果是

2020/09/23 15:07:11 /gopath/pkg/mod/gorm.io/driver/mysql@v1.0.1/mysql.go:52
[info] replacing callback `gorm:update` from /gopath/pkg/mod/gorm.io/driver/mysql@v1.0.1/mysql.go:52

2020/09/23 15:07:11 /home/lbf/work/gowork/gormtest/main.go:23
[0.199ms] [rows:0] SELECT DATABASE()

2020/09/23 15:07:11 /home/lbf/work/gowork/gormtest/main.go:23
[1.066ms] [rows:0] SELECT count(*) FROM information_schema.tables WHERE table_schema = "gormtest" AND table_name = "member" AND table_type = "BASE TABLE"

2020/09/23 15:07:11 /home/lbf/work/gowork/gormtest/main.go:23
[27.842ms] [rows:0] CREATE TABLE `member` (`room_id` bigint,`user_id` bigint,`status` bigint)

在数据库操作软件中也没有主键约束。

go v1.14
gorm.io/driver/mysql v1.0.1
gorm.io/gorm v1.20.1

复现代码.zip

CREATE DATABASE IF NOT EXISTS gormtest DEFAULT CHARSET utf8mb4 COLLATE utf8mb4_general_ci;

package main

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

var db *gorm.DB

func main() {
	dsn := "root:123456@tcp(localhost:3306)/gormtest?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
		Logger: logger.Default.LogMode(logger.Info),
	})
	if err != nil {
		fmt.Println("xx")
		panic(err)
	}

	db.AutoMigrate(
		Member{},
	)

	
}

type Member struct {
	RoomID int64 `json"roomid" gorm:"primaryKey;"`
	UserID int64 `json"userid" gorm:"primaryKey;"`
	Status int64 `json"status" gorm:"column:status;   Type:int;  not null;"`
}

func (Member) TableName() string {
	return "member"
}
@github-actions github-actions bot added the type:missing reproduction steps missing reproduction steps label Sep 23, 2020
@github-actions
Copy link

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.ioSearch Before Asking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants