1

I'm following this tutorial using Golang sqlc with Postgresql, there is an error that says: pq: relation "authors" does not exist exit status 1 whenever I run go run main.go after setting up everything. So the table "authors" is not being generated automatically, how does sqlc generate tables based on the schema.sql file? Do I have to manually copy and paste the code in schema.sql into DBMS SQL script for it to create tables?

bltz
  • 213
  • 1
  • 4
  • 8
  • 1
    I agree that the tutorial is a little unclear on this, but SQLC does not generate database tables for you. For the purposes of the tutorial I'd suggest doing it manually, for larger apps consider the options [in the docs](https://docs.sqlc.dev/en/latest/howto/ddl.html?highlight=migration#handling-sql-migrations). – Brits Jan 16 '23 at 07:18
  • That's the whole point of the library isn't it? To work with your existing databases and SQL scripts you are using to create them. – Richard Huxton Jan 16 '23 at 07:21
  • @RichardHuxton SQLC generates Go code to access the tables (e.g. takes `SELECT * FROM authors WHERE id = $1 LIMIT 1;` and generates a go function that runs that query for you, putting the results into a `struct` etc). It does not create the schema for you; managing migrations is left for other tools (as per [the docs](https://docs.sqlc.dev/en/latest/howto/ddl.html?highlight=migration#handling-sql-migrations)). [Here](https://github.com/kyleconroy/sqlc/tree/main/examples/authors/postgresql) is the full code generated in the tutorial. – Brits Jan 16 '23 at 07:42

0 Answers0