The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
conight

Gorm 使用的问题

  •  
  •   conight · Sep 24, 2020 · 1511 views
    This topic created in 2056 days ago, the information mentioned may be changed or developed.

    现在有俩个表

    type A struct {
    	...
    }
    
    type B struct {
    	...
    	AID string `gorm:"column:a_id;not null;"`
    	...
    }
    

    B 表中有 AID 。 现在实现了以下 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 字段,所以关联不上。这个要怎么实现。

    先谢谢各位好兄弟

    14v45mJPBYJW8dT7
        1
    14v45mJPBYJW8dT7  
       Sep 24, 2020
    使用 db.Exec 执行原生 sql

    或者添加 foreignKey 使用关联
    conight
        2
    conight  
    OP
       Sep 24, 2020
    @rimutuyuan 执行 SQL 需要再定义一个新的 struct
    使用 foreignKey 的话多条记录绑定也需要新的定义么?
    conight
        3
    conight  
    OP
       Sep 24, 2020
    已解决
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5104 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 51ms · UTC 09:44 · PVG 17:44 · LAX 02:44 · JFK 05:44
    ♥ Do have faith in what you're doing.