Skip to content

fix: quote string literals with single quotes in Explain - #242

Open
h2zi wants to merge 2 commits into
go-gorm:masterfrom
h2zi:fix-explain-quoting
Open

fix: quote string literals with single quotes in Explain#242
h2zi wants to merge 2 commits into
go-gorm:masterfrom
h2zi:fix-explain-quoting

Conversation

@h2zi

@h2zi h2zi commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Symptom

Explain wraps string values in double quotes (name = "hello"), but double quotes are identifier quoting in SQLite — SQL copied from the logs misparses the value as a column reference (#197).

Fix

Use standard single-quoted string literals in Explain.

One coupled change is required: GORM embeds string default values into CREATE TABLE DDL through Dialector.Explain (FullDataTypeOfDEFAULT 'hello'), and the DDL parser only stripped double quotes from parsed default values. With Explain fixed in isolation, DefaultValue() would return 'hello' with quotes and every AutoMigrate would try to re-alter the column. The parser now strips single quotes too, and keeps stripping double quotes so tables created by older driver versions still parse.

Tests

  • TestExplainQuotesStrings — literal quoting.
  • TestDefaultValueRoundTrip — default value parses back clean, a second AutoMigrate leaves the DDL untouched, and a legacy double-quoted default still parses.

Fixes #197

🤖 Generated with Claude Code


Merge order (this batch: #241#242#243#244#245#246, plus #234 from the previous batch): all seven are functionally independent and merge cleanly in any order. Several append tests to the same test files, so whichever merges later may show a trivial append-only conflict in migrator_test.go/ddlmod_test.go — I'll rebase the remaining ones promptly after each merge, as before.

Explain wrapped string values in double quotes, which are identifier
quoting in SQLite — SQL copied from the logs misparses the value as a
column reference. Use standard single-quoted string literals instead.

GORM also embeds string default values into CREATE TABLE through
Explain, so the DDL parser now strips single quotes from parsed
default values as well (double quotes are still stripped for tables
created by older driver versions); without that, migrations would stop
being idempotent. Covered by a default-value round-trip test.

Fixes go-gorm#197
Copilot AI review requested due to automatic review settings July 31, 2026 09:33

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Updates the SQLite dialector’s SQL rendering to use correct SQLite string-literal quoting in Explain, and adjusts DDL default-value parsing to keep migrations idempotent when defaults are emitted via Explain.

Changes:

  • Switch Dialector.Explain to use single-quoted string literals (avoids misparsing double quotes as identifiers in SQLite).
  • Update parseDDL default-value extraction to strip string-literal quotes for both new (single-quoted) and legacy (double-quoted) tables.
  • Add tests covering Explain string quoting and default-value round-tripping through AutoMigrate, including legacy double-quoted defaults.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
sqlite.go Explain now emits single-quoted string literals for SQLite correctness.
ddlmod.go Default-value parsing updated to remove surrounding quotes so migrations remain idempotent across versions.
sqlite_test.go Adds coverage for Explain quoting and default parsing / idempotent AutoMigrate.
Suppressed comments (1)

sqlite_test.go:184

  • The result of db.Raw(...).Scan(&after) is not checked. If the query fails, after stays empty and the test may pass/fail for the wrong reason. Capture and assert the error from Scan.
	var after string
	db.Raw("SELECT sql FROM sqlite_master WHERE type='table' AND name='explain_defaults'").Scan(&after)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread sqlite_test.go Outdated
Comment thread ddlmod.go Outdated
Address review feedback: strings.Trim removes every leading/trailing
character from the set, so DEFAULT '"x"' parsed to x instead of "x".
Strip a single matching pair instead, and fail the round-trip test on
query errors.
h2zi added a commit to libtnb/sqlite that referenced this pull request Jul 31, 2026
…traint names

Two fixes surfaced by review feedback on the upstream ports
(go-gorm/sqlite#242, #246):

- strings.Trim removed every leading/trailing quote character from the
  set, so DEFAULT '"x"' parsed to x instead of "x". A trimQuote helper
  strips exactly one matching outer pair.
- compileConstraintRegexp only accepted backquotes and double quotes
  around the constraint name; [name] and 'name' forms now match too.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
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

Successfully merging this pull request may close these issues.

在 gorm.io/driver/sqlite v1.5.6版本hasTable等个别函数或exec出现生成sql语法错误的bug

2 participants