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

Unused struct is being generated into models.go #710

Closed
yeka opened this issue Sep 11, 2020 · 2 comments · Fixed by #2369
Closed

Unused struct is being generated into models.go #710

yeka opened this issue Sep 11, 2020 · 2 comments · Fixed by #2369
Labels
📚 postgresql enhancement New feature or request question Further information is requested
Milestone

Comments

@yeka
Copy link

yeka commented Sep 11, 2020

sqlc.yaml

version: "1"
packages:
  - name: "authors"
    path: "generated/repo/authors"
    queries: "./files/queries/authors.sql"
    schema: "./files/schema/"
    engine: "postgresql"
    emit_json_tags: true
    emit_prepared_queries: false
    emit_interface: false
    emit_exact_table_names: false
  - name: "books"
    path: "generated/repo/books"
    queries: "./files/queries/books.sql"
    schema: "./files/schema/"
    engine: "postgresql"
    emit_json_tags: true
    emit_prepared_queries: false
    emit_interface: false
    emit_exact_table_names: false

The ./files/schema/ directory contains golang-migrate files.

000001_authors.up.sql

CREATE TABLE authors (
    id   BIGSERIAL PRIMARY KEY,
    name text      NOT NULL,
    bio  text
);

000002_books.up.sql

CREATE TABLE books (
    id SERIAL PRIMARY KEY,
    title VARCHAR(120) NOT NULL
);

And directory ./files/queries contains sql queries for sqlc.

authors.sql

-- name: ListAuthors :many
SELECT * FROM authors
ORDER BY name;

books.sql

-- name: ListBooks :many
SELECT * FROM books;

When I run sqlc generate, it will generate ./generated/repo/authors/ and ./generated/repo/books as expected.
When I see the ./generated/repo/authors/models.go and ./generated/repo/books/models.go, both have 2 struct types which is Author and Book.

models.go

// Code generated by sqlc. DO NOT EDIT.

package books

import (
	"database/sql"
)

type Author struct {
	ID   int64          `json:"id"`
	Name string         `json:"name"`
	Bio  sql.NullString `json:"bio"`
}

type Book struct {
	ID    int32  `json:"id"`
	Title string `json:"title"`
}

In books package, Author type in unused, and in authors package Book type is unused.
I assumed it's generated from database schema because both package use the same schema in sqlc.yaml.
Is it possible not to generate unused struct in models.go?

@kyleconroy kyleconroy added 📚 postgresql question Further information is requested labels Sep 12, 2020
@kyleconroy
Copy link
Collaborator

Is it possible not to generate unused struct in models.go?

Yes, we could change the code generation to not output models for unused tables. Note that you still may run into issues if your a query in books.sql returns results from the authors table.

@danilobuerger
Copy link

Hi @kyleconroy are you still considering adding this? I am asking because this has an older milestone set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📚 postgresql enhancement New feature or request question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants