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个回答



问题的症结在于WHERE子句,您需要使用“ OR”而不是“,”。 </ p>

这可能是GORM的问题,您可以为此打开GitHub问题。 您可以使用GORM中的db.Raw()解决此问题。 通常,我更喜欢避免使用ORM。 部分原因是,手动构建SQL查询通常比尝试找出在ORM中进行奇怪处理的方式(对于超出基本CRUD的方法)要容易。</ p>
</ div>

展开原文

原文

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

dongyou6847
dongyou6847 我同意。 我也在其中记录了一些错误。 但是纯SQL是这样。
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐