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

i test example with Mysql, i use Model to use CURD Interface, it return error #47

Closed
VagrantPi opened this issue Jun 27, 2022 · 2 comments
Assignees

Comments

@VagrantPi
Copy link

Your Question

I try to modify the example code to test MySQL sharding

But when I use model interface to access, there will be an error

I change it to Table, is OK

I think this issue like #14 ?

my env:

  • go1.17.1 darwin/amd64
    • dependency
      • gorm.io/driver/mysql v1.3.4
      • gorm.io/gorm v1.23.6
      • github.com/bwmarrin/snowflake v0.3.0 // indirect
      • github.com/longbridgeapp/sqlparser v0.3.1 // indirect
      • github.com/technoweenie/multipartstreamer v1.0.1 // indirect
      • gorm.io/sharding v0.5.1 // indirect
  • mysql:8.0.23

examples/order.go

package main

import (
	"fmt"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
	"gorm.io/sharding"
)

type Order struct {
	ID        int64 `gorm:"primarykey"`
	UserID    int64
	ProductID int64
}

func main() {
	connection := fmt.Sprintf("test:test@tcp(127.0.0.1:3306)/?parseTime=true")
	db, err := gorm.Open(mysql.New(mysql.Config{
		DSN: connection,
	}), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
		Logger: logger.Default.LogMode(logger.Silent),
	})
	if err != nil {
		fmt.Println("err", err)
	}

	db.Exec("USE " + "MyTable")

	for i := 0; i < 4; i += 1 {
		table := fmt.Sprintf("orders_%d", i)
		db.Exec(`DROP TABLE IF EXISTS ` + table)
		db.Table(table).AutoMigrate(&Order{})
	}
	// db.AutoMigrate(&Order{})

	middleware := sharding.Register(sharding.Config{
		ShardingKey:         "user_id",
		NumberOfShards:      4,
		PrimaryKeyGenerator: sharding.PKSnowflake,
	}, "orders")
	db.Use(middleware)

	// this record will insert to orders_02
        // !!!!!!!!!!!!!!!!!!  it returm err: Error 1146: Table 'MyTable.order' doesn't exist
	// err = db.Create(&Order{UserID: 2}).Error
	err = db.Table("orders").Create(&Order{UserID: 2}).Error
	if err != nil {
		fmt.Println("err1", err)
	}

	// this record will insert to orders_03
	err = db.Exec("INSERT INTO orders(user_id) VALUES(?)", int64(3)).Error
	if err != nil {
		fmt.Println("err2", err)
	}

	// this will throw ErrMissingShardingKey error
	err = db.Exec("INSERT INTO orders(product_id) VALUES(1)").Error
	fmt.Println("err3", err)

	// this will redirect query to orders_02
	var orders []Order
        // !!!!!!!!!!!!!!!!!!  it returm err: Error 1146: Table 'MyTable.order' doesn't exist
	// err = db.Model(&Order{}).Where("user_id", int64(3)).Find(&orders).Error
	err = db.Table("orders").Where("user_id", int64(2)).Find(&orders).Error
	if err != nil {
		fmt.Println("err4", err)
	}
	fmt.Printf("%#v\n", orders)

	// Raw SQL also supported
	db.Raw("SELECT * FROM orders WHERE user_id = ?", int64(3)).Scan(&orders)
	fmt.Printf("%#v\n", orders)

	// this will throw ErrMissingShardingKey error
        // !!!!!!!!!!!!!!!!!!  it returm err: Error 1146: Table 'MyTable.order' doesn't exist
	// err = db.Model(&Order{}).Where("product_id", "1").Find(&orders).Error
	err = db.Table("orders").Where("product_id", "1").Find(&orders).Error
	fmt.Println("err5", err)

	// Update and Delete are similar to create and query
	err = db.Exec("UPDATE orders SET product_id = ? WHERE user_id = ?", 2, int64(3)).Error
	fmt.Println("err6", err) // nil
	err = db.Exec("DELETE FROM orders WHERE product_id = 3").Error
	fmt.Println("err7", err) // ErrMissingShardingKey
}

see // !!!!!!!!!!!!!!!!!! it returm err: Error 1146: Table 'MyTable.order' doesn't exist

The document you expected this should be explained

Expected answer

i want use Model to access, not Table

@huacnlee
Copy link
Collaborator

Remove SingularTable: true

@VagrantPi
Copy link
Author

thanks

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

No branches or pull requests

3 participants