Closed
Description
Not sure if this should be flagged as a bug or as a feature request.
At the moment, it is not possible to use custom types (i.e. non simple types) as primary key when joining (for example sql.NullInt64
or similar). The data will be extracted but the grouping will not be correct.
Code snippet
The following is the example in the Wiki but with the primary keys as sql.NullInt64
instead of int32
// Address struct has the same name and fields as auto-generated model struct
type Address struct {
ID sql.NullInt64 `sql:"primary_key"`
AddressLine string
}
type MyCustomer struct {
ID sql.NullInt64 `sql:"primary_key"`
LastName *string
Address Address
}
type MyCity struct {
ID sql.NullInt64 `sql:"primary_key"`
Name string
Customers []MyCustomer
}
dest2 := []MyCity{}
stmt2 :=
SELECT(
City.CityID.AS("my_city.id"), // snake case
City.City.AS("myCity.Name"), // camel case
Address.AddressID, // No need for aliasing.
Address.Address, // Default aliasing still works.
Customer.CustomerID.AS("My_Customer.id"), //mixed case
Customer.LastName.AS("my customer.last name"), //with spaces
).FROM(
City.
INNER_JOIN(Address, Address.CityID.EQ(City.CityID)).
INNER_JOIN(Customer, Customer.AddressID.EQ(Address.AddressID)),
).WHERE(
City.City.EQ(String("London")).OR(City.City.EQ(String("York"))),
).ORDER_BY(
City.CityID, Address.AddressID, Customer.CustomerID,
)
err := stmt2.Query(db, &dest2)
Instead of extracting 2 cities with 2 and 1 customers each respectively, it will extract 3 cities with 1 customer each. The first city will be repeated twice (reflecting how the join works). In code:
[
{
"ID": 312,
"Name": "London",
"Customers": [
{
"ID": 252,
"LastName": "Hoffman",
"Address": {
"ID": 256,
"AddressLine": "1497 Yuzhou Drive"
}
}
]
},
{
"ID": 312,
"Name": "London",
"Customers": [
{
"ID": 512,
"LastName": "Vines",
"Address": {
"ID": 517,
"AddressLine": "548 Uruapan Street"
}
}
]
},
{
"ID": 589,
"Name": "York",
"Customers": [
{
"ID": 497,
"LastName": "Sledge",
"Address": {
"ID": 502,
"AddressLine": "1515 Korla Way"
}
}
]
}
]
The proposed #197 fixes this.
Activity