kayak / pypika

PyPika is a python SQL query builder that exposes the full richness of the SQL language using a syntax that reflects the resulting query. PyPika excels at all sorts of SQL queries but is especially useful for data analysis.
http://pypika.readthedocs.io/en/latest/
Apache License 2.0
2.43k stars 293 forks source link

Using a table alias on delete generates invalid SQL syntax #718

Open tijm-wolters opened 1 year ago

tijm-wolters commented 1 year ago

Environment

pypika version: Latest master (30574f997c80851f7e940ad09a63e14a98871dd3) SQL dialect tested: MySQL

Problem

I was working with the pypika library and found out some functionality around table aliases doesn't work as expected, and will return syntactical errors in the resulting query.

The correct syntax for aliases on delete are

# Either:
DELETE c FROM customers AS c;

# Or:
DELETE c FROM customers c;

Example

Expected:

customers_table = Table('customers').as_('c')
customers_table.delete() # DELETE "c" FROM "customers" AS "c";

Actual:

customers_table = Table('customers').as_('c')
query = customers_table.delete() # DELETE FROM "customers" "c";