Skip to content

interface{} generated but I think sqlc should generate NullString #1095

Description

@kevinburke1

I have this query:

-- name: GetControllerWithCompanySlug :one 
SELECT *, (
    SELECT textfield
    FROM companies
    WHERE controllers.company_sid = companies.sid
) AS "company_slug"
FROM controllers WHERE controllers.name = $1;

Controller has a nullable foreign key to companies. I would expect that this would return a sql.NullString but instead the type of "textfield" is interface{}.

Activity

  1. kyleconroy commented on Aug 11, 2021

    @kyleconroy
    Collaborator

    Fixed in #1113

    create table companies (sid text not null, description text);
    
    create table controllers (sid text not null, company_sid text, name text);
    
    -- name: GetCompanySlug :many
    SELECT *, (
        SELECT description
        FROM companies
        WHERE controllers.company_sid = companies.sid
    ) AS "company_slug"
    FROM controllers WHERE controllers.name = $1;
    const getCompanySlug = `-- name: GetCompanySlug :many
    SELECT sid, company_sid, name, (
        SELECT description
        FROM companies
        WHERE controllers.company_sid = companies.sid
    ) AS "company_slug"
    FROM controllers WHERE controllers.name = $1
    `
    
    type GetCompanySlugRow struct {
    	Sid         string
    	CompanySid  sql.NullString
    	Name        sql.NullString
    	CompanySlug sql.NullString
    }
    
    func (q *Queries) GetCompanySlug(ctx context.Context, name sql.NullString) ([]GetCompanySlugRow, error) {
    	rows, err := q.db.QueryContext(ctx, getCompanySlug, name)
    	if err != nil {
    		return nil, err
    	}
    	defer rows.Close()
    	var items []GetCompanySlugRow
    	for rows.Next() {
    		var i GetCompanySlugRow
    		if err := rows.Scan(
    			&i.Sid,
    			&i.CompanySid,
    			&i.Name,
    			&i.CompanySlug,
    		); err != nil {
    			return nil, err
    		}
    		items = append(items, i)
    	}
    	if err := rows.Close(); err != nil {
    		return nil, err
    	}
    	if err := rows.Err(); err != nil {
    		return nil, err
    	}
    	return items, nil
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions