dongqian1925 2017-02-05 06:24
浏览 112

GORM many2很多预加载错误

Currently using GORM to connect to two databases: POSTGRES AND sqlite (using a code switch to choose which one to use). I have a 2 database tables defined in my schema that look like this:

type TableClient struct { 
    Model
    Synchronised bool
    FacilityID string `gorm:"primary_key"`
    Age                       int
    ClientSexID               int  
    MaritalStatusID           int
    SpecificNeeds             []TableOptionList`gorm:"many2many:options_specific_needs"`
}

type TableOptionList struct {
    ID        int `gorm:"primary_key"`
    Name    string
    Value   string
    Text    string
    SortKey int
  }

Previously, I would preload related table with code like this:

var dbClient TableClient
Db.Where("facility_id = ? AND client_id = ? AND id = ?;", URLFacilityID, URLClientID, URLIncidentID).
   Preload("ClientSex").
   Preload("MaritalStatus").
   Preload("CareTakerRelationShip").
   Preload("HighestLevelOfEducation").
   Preload("Occupation").
   Preload("SpecificNeeds").
   First(&dbClient)

Now that lookup fails with a syntax error and when I look at the SQL generated, it shows the following SQL is generated:

SELECT * FROM "table_option_lists" INNER JOIN "options_specific_needs" ON       "options_specific_needs"."table_option_list_id" = "table_option_lists"."id" WHERE (("options_specific_needs"."table_client_id","options_specific_needs"."table_client_facility_id") IN (('one','LS034')))

Pasting that into the sqlite console also fails with the same error: (near ",": syntax error)

  • 写回答

1条回答 默认 最新

  • dongzhan7253 2017-02-11 15:12
    关注

    The crux of your issue is in your WHERE clause, you need to use "OR", not ",".

    This is likely an issue with GORM and you could open a GitHub issue with them for this. You can get around the issue using db.Raw() from GORM. In general, I prefer to avoid ORMs. Part of the reason is it is often easier to just build an SQL query by hand than try to figure out the strange way to do it in the ORM (for things beyond basic CRUD).

    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP