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

Quote table name in SQL exporter #6586

Open
timtomch opened this issue May 3, 2024 · 3 comments
Open

Quote table name in SQL exporter #6586

timtomch opened this issue May 3, 2024 · 3 comments
Labels
SQL/database Integration with SQL databases in general Type: Bug Issues related to software defects or unexpected behavior, which require resolution.

Comments

@timtomch
Copy link
Sponsor

timtomch commented May 3, 2024

The SQL exporter tries to clean up column names and encloses them in double quotes, but it doesn't appear to do so for the table name. This is an issue e.g. when project names only contain digits, which MySQL won't accept as table names unless quoted.

To Reproduce

Steps to reproduce the behavior:

  1. Create a project with a name that includes only digits
  2. Export project as SQL

Current Results

The current (OR 3.8.0) SQL export quotes column names but not the table name:

CREATE TABLE 20240503 (
"_____ABC" VARCHAR(255) NULL,
"2211" VARCHAR(255) NULL,
"_22ABC" VARCHAR(255) NULL
);
INSERT INTO 20240503 ("_____ABC","2211","_22ABC") VALUES 
( '111','222','333' ),
( 'zzz','1212 ','1212' )

Expected Behavior

Also quote the table name:

CREATE TABLE "20240503" (
"_____ABC" VARCHAR(255) NULL,
"2211" VARCHAR(255) NULL,
"_22ABC" VARCHAR(255) NULL
);
INSERT INTO "20240503" ("_____ABC","2211","_22ABC") VALUES 
( '111','222','333' ),
( 'zzz','1212 ','1212' )

Additional context

Adding double quotes around the table name would be consistent with the current quoting of column names but wouldn't address the issue of the resulting SQL being rejected by MySQL as MySQL expects backticks instead of double quotes.

A potential solution to this issue would be to add an option in the UI for the user to specify which character to escape table and column names with, as initially proposed in #4716 but subsequently rejected in favour of implementing the SQL standard of double quotes only.

This was discussed on the forum after a user reported having trouble exporting to MySQL.

@timtomch timtomch added Status: Pending Review Indicates that the issue or pull request is awaiting review by project maintainers or collaborators Type: Bug Issues related to software defects or unexpected behavior, which require resolution. labels May 3, 2024
@thadguidry
Copy link
Member

thadguidry commented May 8, 2024

I tend to agree. We should add a toggle to

  • Use ANSI QUOTES

which once checked, would wrap double quotes around identifiers.
when unchecked, it would use backticks.

https://dev.mysql.com/doc/refman/8.4/en/identifiers.html

@timtomch And the default should be... unchecked, you think?

@thadguidry thadguidry added SQL/database Integration with SQL databases in general and removed Status: Pending Review Indicates that the issue or pull request is awaiting review by project maintainers or collaborators labels May 8, 2024
@tfmorris tfmorris changed the title Escape table name in SQL exporter Quote table name in SQL exporter May 8, 2024
@tfmorris
Copy link
Member

tfmorris commented May 9, 2024

I changed "escape" to "quote" to follow common terminology.

It looks like there are at least three different conventions for quoting what SQL calls "delimited identifiers":

  1. "ANSI Standard"
  2. [MS SQL Server]
  3. MySQL

In all cases, the quote character can be included in the identifier by doubling (ie escaping) it. I don't know if we have other uses of identifiers besides column names and table names, but, if so, they should follow the same quoting rules.

@timtomch
Copy link
Sponsor Author

timtomch commented May 9, 2024

Thanks for figuring this out! I think that despite MySQL being quite common, the default should still be the ANSI standard, no?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
SQL/database Integration with SQL databases in general Type: Bug Issues related to software defects or unexpected behavior, which require resolution.
Projects
None yet
Development

No branches or pull requests

3 participants