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

SQLite generation fails when using Quoted Identifiers #1817

Closed
caleblloyd opened this issue Aug 23, 2022 · 2 comments · Fixed by #2556
Closed

SQLite generation fails when using Quoted Identifiers #1817

caleblloyd opened this issue Aug 23, 2022 · 2 comments · Fixed by #2556

Comments

@caleblloyd
Copy link

caleblloyd commented Aug 23, 2022

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

@caleblloyd caleblloyd added bug Something isn't working triage New issues that hasn't been reviewed labels Aug 23, 2022
@caleblloyd
Copy link
Author

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.

@kyleconroy kyleconroy added 💻 linux 🔧 golang 📚 sqlite and removed triage New issues that hasn't been reviewed labels Aug 28, 2022
@joshsziegler
Copy link

joshsziegler commented Apr 6, 2023

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.

orisano added a commit to orisano/sqlc that referenced this issue Aug 1, 2023
orisano added a commit to orisano/sqlc that referenced this issue Aug 1, 2023
kyleconroy pushed a commit that referenced this issue Aug 2, 2023
* fix(engine/sqlite): support quoted identifier

close #1817

* test: add endtoend
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants