现在有俩个表
type A struct { ... } type B struct { ... AID string `gorm:"column:a_id;not null;"` ... }
B
表中有 A
的 ID
。 现在实现了以下 SQL
。
select a.*, ( select count(*) from b where b.a_id = a.id ) as b_count from a
Gorm
db = db.Select(`a.*, ( select count(*) from b where b.a_id = a.id ) as b_count`)
但 A
表中没有 b_count
字段,所以关联不上。这个要怎么实现。
先谢谢各位好兄弟
1 14v45mJPBYJW8dT7 2020-09-24 17:27:53 +08:00 使用 db.Exec 执行原生 sql 或者添加 foreignKey 使用关联 |
2 conight OP @rimutuyuan 执行 SQL 需要再定义一个新的 struct 使用 foreignKey 的话多条记录绑定也需要新的定义么? |
3 conight OP 已解决 |