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

When upgrade to 1.11.0, nullable fields not correct in left join #1349

Closed
damla-unal opened this issue Dec 20, 2021 · 2 comments
Closed

When upgrade to 1.11.0, nullable fields not correct in left join #1349

damla-unal opened this issue Dec 20, 2021 · 2 comments

Comments

@damla-unal
Copy link

damla-unal commented Dec 20, 2021

Version

1.11.0

What happened?

When work with v1.10, left join is run correctly.

When the code was generated with v1.10, the left join struct was as follows :

type GetItemByUniqueIdentifierRow struct {
	ID               int64
	UniqueIdentifier string
	Title            string
	Content          string
	MetadataID       sql.NullInt64
	MetadataRate     sql.NullInt64
	ID_2             sql.NullInt64
	Rate             sql.NullInt64
	Name             sql.NullString
	ItemID           sql.NullInt64
}

When the code was generated with v1.11

type GetItemByUniqueIdentifierRow struct {
	ID               int64
	UniqueIdentifier string
	Title            string
	Content          string
	MetadataID       sql.NullInt64
	MetadataRate     sql.NullInt64
	ID_2             int64
	Rate             int64
	Name             string
	ItemID           int64
}

You can see that the joined table fields (Rate, Name, ItemID..), should have Null* types but when used with v1.11 this fields seem like non-nullable(int64 instead of NullInt64).

In fact, when doing left join, if we use alias, it detects nullable fields correctly, if we do not use it and we say that it takes all columns like c.* without using alias, it shows the fields of the join table incorrectly.

If we look at the join query, the alias was used for the id and rate fields of the joined table, and these fields became nullable when the code was generated. It is true.
When we use c.* to get all columns of the joined table, all fields are generated as non-nullable. There seems to be a bug here about using c.*

SQL tables and go models are below.

Relevant log output

No response

Database schema

CREATE TABLE item_metadata  (
          id bigserial PRIMARY KEY,
          name TEXT NOT NULL DEFAULT '',
          rate BIGINT NOT NULL,
          item_id BIGINT REFERENCES items (id) ON DELETE CASCADE
);

CREATE TABLE item  (
          id bigserial PRIMARY KEY,
          title TEXT NOT NULL DEFAULT '',
          content TEXT NOT NULL DEFAULT '',
          unique_identifier TEXT NOT NULL,
  UNIQUE(unique_identifier)
);


-- name: GetItemByUniqueIdentifier :one
SELECT t.*,
    c.id as metadata_id,
    c.rate as metada_rate,
    c.*
FROM item t
    LEFT JOIN item_metadata c ON c.item_id = t.id
WHERE unique_identifier = $1;


Go models : 

type item struct {
	UniqueIdentifier string
	Title         string
	Content       string

	ItemMetadata    *metadata.ItemMetadata

}

type ItemMetadata struct {
	Name              string 
        Rate              int

}

SQL queries

-- name: GetItemByUniqueIdentifier :one
SELECT t.*,
    c.id as metadata_id,
    c.rate as metadata_rate,
    c.*
FROM item t
    LEFT JOIN item_metadata c ON c.item_id = t.id
WHERE unique_identifier = $1;

Configuration

No response

Playground URL

No response

What operating system are you using?

macOS

What database engines are you using?

PostgreSQL

What type of code are you generating?

Go

@damla-unal damla-unal added bug Something isn't working triage New issues that hasn't been reviewed labels Dec 20, 2021
@damla-unal damla-unal changed the title When upgrade to 11.1.0, nullable fields not correct in left join When upgrade to 1.11.0, nullable fields not correct in left join Dec 20, 2021
@kyleconroy kyleconroy added 📚 postgresql 💻 darwin 🔧 golang and removed triage New issues that hasn't been reviewed labels Jan 26, 2022
@sbres
Copy link
Contributor

sbres commented Feb 26, 2022

I've just upgraded to 1.12.0 and I found the same problem.

@kdubovikov
Copy link

Seems to be working in the current main branch build.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants