Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QRM] It is not possible to use non simple types as PKs in joins #198

Closed
emarj opened this issue Dec 16, 2022 · 0 comments
Closed

[QRM] It is not possible to use non simple types as PKs in joins #198

emarj opened this issue Dec 16, 2022 · 0 comments
Labels
bug Something isn't working

Comments

@emarj
Copy link
Contributor

emarj commented Dec 16, 2022

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.

@emarj emarj added the bug Something isn't working label Dec 16, 2022
@emarj emarj closed this as completed Dec 19, 2022
@go-jet go-jet added this to the Version 2.10.0 milestone Apr 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants