Advertisement
pda0

Untitled

Oct 7th, 2021
1,499
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 5.82 KB | None | 0 0
  1. gorm.io/gorm v1.21.15
  2. модели:
  3.  
  4. type Suppliers struct {
  5.     SupplierID   int32  `gorm:"column:SupplierID;primaryKey"`
  6.     CompanyName  string `gorm:"column:CompanyName"`
  7.     ContactName  string `gorm:"column:ContactName"`
  8.     ContactTitle string `gorm:"column:ContactTitle"`
  9.     Address      string `gorm:"column:Address"`
  10.     City         string `gorm:"column:City"`
  11.     Region       string `gorm:"column:Region"`
  12.     PostalCode   string `gorm:"column:PostalCode"`
  13.     Country      string `gorm:"column:Country"`
  14.     Phone        string `gorm:"column:Phone"`
  15.     Fax          string `gorm:"column:Fax"`
  16.     HomePage     string `gorm:"column:HomePage"`
  17. }
  18.  
  19. type Categories struct {
  20.     CategoryID   int32  `gorm:"column:CategoryID;primaryKey"`
  21.     CategoryName string `gorm:"column:CategoryName"`
  22.     Description  string `gorm:"column:Description"`
  23.     Picture      []byte `gorm:"column:Picture"`
  24. }
  25.  
  26. type Products struct {
  27.     ProductID       int32        `gorm:"column:ProductID;primaryKey"`
  28.     ProductName     string       `gorm:"column:ProductName"`
  29.     SupplierID      int32        `gorm:"column:SupplierID"`
  30.     CategoryID      int32        `gorm:"column:CategoryID"`
  31.     QuantityPerUnit string       `gorm:"column:QuantityPerUnit"`
  32.     UnitPrice       float32      `gorm:"column:UnitPrice"`
  33.     UnitsInStock    int32        `gorm:"column:UnitsInStock"`
  34.     UnitsOnOrder    int32        `gorm:"column:UnitsOnOrder"`
  35.     ReorderLevel    int32        `gorm:"column:ReorderLevel"`
  36.     Discontinued    bool         `gorm:"column:Discontinued"`
  37.     Suppliers       []Suppliers  `gorm:"foreignKey:SupplierID"`
  38.     Categories      []Categories `gorm:"foreignKey:CategoryID"`
  39. }
  40.  
  41. вызов через joins:
  42.  
  43. var products []Products
  44. db.Debug().
  45.     Joins("Suppliers").
  46.     Joins("Categories").
  47.     Find(&products, "[Products].[SupplierID] IN ?", []int32{3, 4})
  48.  
  49. fmt.Printf("%#v\n", products)
  50.  
  51. сгенерированный sql, в лог не попадает, достал через отладчик, в базе выполняется
  52. SELECT [products].[ProductID],[products].[ProductName],[products].[SupplierID],[products].[CategoryID],[products].[QuantityPerUnit],[products].[UnitPrice],[products].[UnitsInStock],[products].[UnitsOnOrder],[products].[ReorderLevel],[products].[Discontinued],[Suppliers].[SupplierID] AS [Suppliers__SupplierID],[Suppliers].[CompanyName] AS [Suppliers__CompanyName],[Suppliers].[ContactName] AS [Suppliers__ContactName],[Suppliers].[ContactTitle] AS [Suppliers__ContactTitle],[Suppliers].[Address] AS [Suppliers__Address],[Suppliers].[City] AS [Suppliers__City],[Suppliers].[Region] AS [Suppliers__Region],[Suppliers].[PostalCode] AS [Suppliers__PostalCode],[Suppliers].[Country] AS [Suppliers__Country],[Suppliers].[Phone] AS [Suppliers__Phone],[Suppliers].[Fax] AS [Suppliers__Fax],[Suppliers].[HomePage] AS [Suppliers__HomePage],[Categories].[CategoryID] AS [Categories__CategoryID],[Categories].[CategoryName] AS [Categories__CategoryName],[Categories].[Description] AS [Categories__Description],[Categories].[Picture] AS [Categories__Picture] FROM (([products] LEFT JOIN [suppliers] [Suppliers] ON [products].[ProductID] = [Suppliers].[SupplierID]) LEFT JOIN [categories] [Categories] ON [products].[ProductID] = [Categories].[CategoryID]) WHERE [Products].[SupplierID] IN (2,3)
  53.  
  54. panic: reflect: call of reflect.Value.Field on slice Value
  55. Stack:
  56.      2  0x0000000000557e3e in testing.tRunner.func1.2
  57.          at D:/Program Files/Go/src/testing/testing.go:1209
  58.      3  0x0000000000557510 in testing.tRunner.func1
  59.          at D:/Program Files/Go/src/testing/testing.go:1212
  60.      5  0x000000000050e7d0 in reflect.Value.Field
  61.          at D:/Program Files/Go/src/reflect/value.go:1185
  62.      6  0x000000000062c84c in gorm.io/gorm/schema.(*Field).setupValuerAndSetter.func4
  63.          at D:/usergo/pkg/mod/gorm.io/gorm@v1.21.15/schema/field.go:445
  64.      7  0x000000000062a6ce in gorm.io/gorm/schema.(*Field).setupValuerAndSetter.func9
  65.          at D:/user/go/pkg/mod/gorm.io/gorm@v1.21.15/schema/field.go:570
  66.      8  0x000000000062be10 in gorm.io/gorm/schema.(*Field).setupValuerAndSetter.func7
  67.          at D:/user/go/pkg/mod/gorm.io/gorm@v1.21.15/schema/field.go:517
  68.      9  0x000000000062b00d in gorm.io/gorm/schema.(*Field).setupValuerAndSetter.func9
  69.          at D:/user/go/pkg/mod/gorm.io/gorm@v1.21.15/schema/field.go:614
  70.     10  0x000000000062be10 in gorm.io/gorm/schema.(*Field).setupValuerAndSetter.func7
  71.          at D:/user/go/pkg/mod/gorm.io/gorm@v1.21.15/schema/field.go:517
  72.     11  0x000000000062b00d in gorm.io/gorm/schema.(*Field).setupValuerAndSetter.func9
  73.          at D:/user/go/pkg/mod/gorm.io/gorm@v1.21.15/schema/field.go:614
  74.     12  0x0000000000668af9 in gorm.io/gorm.Scan
  75.          at D:/user/go/pkg/mod/gorm.io/gorm@v1.21.15/scan.go:181
  76.     13  0x0000000000693615 in gorm.io/gorm/callbacks.Query
  77.          at D:/user/go/pkg/mod/gorm.io/gorm@v1.21.15/callbacks/query.go:25
  78.     14  0x000000000064f073 in gorm.io/gorm.(*processor).Execute
  79.          at D:/user/go/pkg/mod/gorm.io/gorm@v1.21.15/callbacks.go:130
  80.     15  0x00000000006588b4 in gorm.io/gorm.(*DB).Find
  81.          at D:/user/go/pkg/mod/gorm.io/gorm@v1.21.15/finisher_api.go:166
  82.     16  0x00000000006a694b in dev.iec.com.ru/febest/msaccess.TestSelectComplex
  83.          at D:/user/msaccess/msaccess_test.go:461
  84.     17  0x00000000005570bb in testing.tRunner
  85.          at D:/Program Files/Go/src/testing/testing.go:1259
  86.     18  0x0000000000558679 in testing.(*T).Run·dwrap·21
  87.          at D:/Program Files/Go/src/testing/testing.go:1306
  88.  
  89. если заменить на preload
  90. db.Debug().
  91.     Preload("Suppliers").
  92.     Preload("Categories").
  93.     Find(&products, "[Products].[SupplierID] IN ?", []int32{3, 4})
  94.  
  95. генерируются запросы
  96. SELECT * FROM [categories] WHERE [categories].[CategoryID] IN (6,7,8,9,10,74)
  97. SELECT * FROM [suppliers] WHERE [suppliers].[SupplierID] IN (6,7,8,9,10,74)
  98. SELECT * FROM [products] WHERE [Products].[SupplierID] IN (3,4)
  99.  
  100. всё работает, в консоль уходят нормальные данные
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement