go-gorm / sharding

High performance table sharding plugin for Gorm.
MIT License
263 stars 55 forks source link

Error 1054 (42S22): Unknown column 'user_follow.deleted_at' in 'where clause' #100

Closed zishiguo closed 5 months ago

zishiguo commented 1 year ago

GORM Playground Link

568

Description

I also met this question sharding Error 1054: Unknown column 'ny_order.deleted_at' in 'where clause'

detailed error as follows

2023/02/19 20:57:18 testing mysql...
=== RUN   TestGORM

2023/02/19 20:57:18 /Users/zhengjb/go/src/github.com/zishiguo/playground/main_test.go:13 Error 1054 (42S22): Unknown column 'user_follow.deleted_at' in 'where clause'
[2.526ms] [rows:0] SELECT * FROM `user_follow` WHERE user_id = 79535114761158382 AND `user_follow`.`deleted_at` IS NULL
    main_test.go:14: Failed, got error: Error 1054 (42S22): Unknown column 'user_follow.deleted_at' in 'where clause'
--- FAIL: TestGORM (0.00s)

FAIL

Process finished with the exit code 1
huacnlee commented 1 year ago

It poses by func (UserFollow) TableName() string, Gorm Sharding my not full supports of it.

zishiguo commented 1 year ago

@huacnlee so I avoid to use soft delete feature when using sharding with mysql ?

huacnlee commented 1 year ago

No, You can try to remove custom table name func. To use the default table name.

In default, the default table name isuser_follows not user_follow.

zishiguo commented 1 year ago

@huacnlee thanks, I adjust table name from user_follow touser_follows. I have a try with your solution.

zishiguo commented 1 year ago

@huacnlee The problem still exists. It shows same error.

zishiguo commented 1 year ago

@huacnlee use gorm default table name code. 568

huacnlee commented 1 year ago

https://github.com/go-gorm/playground/pull/568/files#r1112867218

254408599 commented 11 months ago

你可以这样改 找到sharding.go文件,定位到412行 stmt.FromItems = newTable stmt.OrderBy = replaceOrderByTableName(stmt.OrderBy, tableName, newTable.Name.Name) stQuery = stmt.String() 把这3行注释掉,然后新增 stQuery = strings.ReplaceAll(stmt.String(),tableName,newTable.Name.Name)

下面的更新和删除也需要这样改,不然更新和删除也会失败 更新和删除需要注释 stmt.TableName = newTable stQuery = stmt.String() 新增 stQuery = strings.ReplaceAll(stmt.String(),tableName,newTable.Name.Name)

zishiguo commented 11 months ago

@254408599 thanks

zishiguo commented 5 months ago

@254408599 Can you launch a PR ?

zishiguo commented 5 months ago

@huacnlee