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

sql.Open returns *sql.DB not DBTX #71

Closed
KINGSABRI opened this issue Nov 19, 2022 · 5 comments
Closed

sql.Open returns *sql.DB not DBTX #71

KINGSABRI opened this issue Nov 19, 2022 · 5 comments

Comments

@KINGSABRI
Copy link

KINGSABRI commented Nov 19, 2022

Hi,
Thanks for the great tutorial.

In the following code, New function expects DBTX. However, sql.open returns *sql.DB and error type therefore the code fails

	conn, err := sql.Open(dbDriver, dbSource)
	if err != nil {
		log.Fatal("cannot connect to db: ", err)
	}

	testQueries = New(conn) // <<--- type fails here 

The same issue will happen later

type Store struct {
	*Queries
	db *sql.DB
}

func NewStore(db *sql.DB) *Store {
	return &Store{
		db:      db,
		Queries: New(db), // <-- here
	}
}
cannot use db (variable of type *sql.DB) as DBTX value in argument to New: *sql.DB does not implement DBTX (wrong type for method Exec)
		have Exec(query string, args ...any) (sql.Result, error)
		want Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, 

Thanks!

@pawlobanano
Copy link

pawlobanano commented Dec 7, 2022

Hi @KINGSABRI, is it still a valid issue on your side? - have you managed to fix it already?

In the line
_func NewStore(db *sql.DB) *Store {_
there is no need for a pointer to the Store interface.

func NewStore(db *sql.DB) Store {
I encourage you to use a static profiler - when I added '*', I immediately got an error, in my case in VSCode.

@KINGSABRI
Copy link
Author

Hi @pawlobanano , thank you for your reply. I'm still having this issue.

I had a look at your code, if you removed the point, you can't reference it (&SQLStore)

func NewStore(db *sql.DB) Store {
return &SQLStore{
db: db,
Queries: New(db),
}
}

@techschool
Copy link
Owner

techschool commented Dec 8, 2022

@KINGSABRI You can still reference it, because Store is just an interface, while SQLStore is a struct that implements the interface. They're basically 2 separate entities.
It should be fine as long as SQLStore implements all methods required by the Store interface.
And there's no point in using a pointer to an interface (as it's not a real object in the memory).
You can read more about the interface here: https://gobyexample.com/interfaces
And play around with the interface here: https://go.dev/tour/methods/9

@Ultra317
Copy link

Ultra317 commented Mar 15, 2024

did you chose pgx/v5 in the sqlc.yaml? I have the same problem like you before

version: "2"
sql:
  - engine: "postgresql"            
    queries: "./db/query"          
    schema: "./db/migration"        
    gen:
      go:
        package: "db"               
        out: "./db/sqlc"            
        sql_package: "pgx/v5"
        emit_json_tags: true       
        emit_prepared_queries: false 
        emit_exact_table_names: false 

then i change it into

version: "1"
packages:
  - name: "db"
    path: "./db/sqlc"
    queries: "./db/query/"
    schema: "./db/migration/"
    engine: "postgresql"
    emit_db_tags: true
    emit_prepared_queries: false
    emit_interface: false
    emit_exact_table_names: false

and it just works, i think it may cause by the different sql_packge used between version 1 and version 2

@atinyfairy
Copy link

atinyfairy commented Mar 28, 2024

change sql_package from pgx/v5 to database/sql
`
sql:

  • engine: "postgresql"
    queries: "./db/query/"
    schema: "./db/migration/"
    name: "db"
    database:
    uri: "postgres://root:eva02@localhost:5432/simple_bank"
    gen:
    go:
    package: "simplebank"
    out: "./db/sqlc"
    sql_package: "database/sql"
    emit_json_tags: true
    emit_prepared_queries: true
    emit_interface: false
    emit_exact_table_names: false`

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

No branches or pull requests

5 participants