sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

copyfrom in MySQL with sqlc does not recognize reserved keywords and does not escape column names manually or automatically

Open Umamad opened this issue 6 months ago • 1 comments

Summary

Using :copyfrom in MySQL with sqlc and column names that are reserved keywords like create, read, update, delete fails with a MySQL syntax error, even if escaped.

Expected behavior

sqlc should either:

  • Automatically escape column names in the generated LOAD DATA LOCAL INFILE statement, or
  • Respect backticks in -- columns: directive

Actual behavior

The generated Go code produces a raw SQL string like:

LOAD DATA LOCAL INFILE 'Reader::xyz' INTO TABLE `role_system` (...) (role_id, system_id, create, read, update, delete)

Umamad avatar Jun 08 '25 17:06 Umamad

Work around

I made a work around using sed command run this command after sqlc generate command. this command works in bash sed -i '' 's/(role_id, system_id, create, read, update, delete)/(`role_id`, `system_id`, `create`, `read`, `update`, `delete`)/g' data/repository/*.go

I also use make file and just add a command for it:

// Makefile
generate:
	sqlc generate
	sed -i '' 's/(role_id, system_id, create, read, update, delete)/(`role_id`, `system_id`, `create`, `read`, `update`, `delete`)/g' data/repository/*.go

GG

Umamad avatar Jun 08 '25 17:06 Umamad