sqlc
sqlc copied to clipboard
SQLite generation fails when using Quoted Identifiers
Version
1.15.0
What happened?
Failure when SQLite identifiers are quoted with double quotes, for example quoting a table name as "test" or a column name as "id"
Relevant log output
sqlc generate failed.
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.15.0
package db
import (
)
type "Test" struct {
"Id" string
}
Database schema
-- Example queries for sqlc
CREATE TABLE "test"
(
"id" TEXT NOT NULL
);
SQL queries
-- name: TestList :many
SELECT * FROM "test";
Configuration
{
"version": "1",
"packages": [
{
"path": "db",
"engine": "sqlite",
"schema": "query.sql",
"queries": "query.sql"
}
]
}
Playground URL
https://play.sqlc.dev/p/1f469b2debde2c474439045390885b2bc9e9c9bcba0f9f2ecb78a8515b7afe07
What operating system are you using?
Linux
What database engines are you using?
SQLite
What type of code are you generating?
Go
A workaround is to remove quotes around the identifier, but this will cause issues if the identifier is a reserved SQLite Keyword.
I would be happy to offer a PR if someone could point me in the right direction.
For others who may find this thread.
I couldn't figure out why my table names had double quotes, because I didn't use them in CREATE TABLE or subsequent ALTER TABLE commands. It seems that SQLite does this when you run ALTER TABLE x RENAME TO y; and will thereafter show the table name with double quotes when dumping your schema via .schema. See this thread for more.
This means I can't easily remove the quotes around identifiers to get around the issue with sqlc. I could use sed to remove all quotes, but there are some strings that need them for default values. The better solution would be to fix sqlc.
I looked upstream at their SQLite grammar, which uses a slightly different method for handling case than this repo does, but I don't see anything different with quoting. Using their Lab feature and and grammar shows quoted identifiers working just fine: http://lab.antlr.org/ Select SQLite for the Parser/Lexer, and null_test.sql for the example. The table name is quoted and is correctly parsed. This makes me think the bug lies not in the grammar, but in sqlc.
I'm still looking, because I would like to use this project but I thought I'd share my findings so far.