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).

    评论

报告相同问题?

悬赏问题

  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64