haxetink / tink_sql

SQL embedded into Haxe
MIT License
53 stars 16 forks source link

Wrong DELETE query: the `@:table` alias is missing #122

Closed cedx closed 3 years ago

cedx commented 4 years ago

I have the following database class (using the MySQL driver on PHP target with MariaDB):

import tink.sql.Database;

class Db extends Database {
  @:table("sessions") final session: SessionData;
}

And the following query:

db.session.delete({where: s -> s.updatedAt <= Date.now().delta(-duration)});

The SQL produced is:

DELETE FROM `sessions` WHERE NOT (`session`.`updatedAt` > '2020-11-16 16:34:35')

... and this SQL fails with this error (because the table alias is missing):

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'session.updatedAt' in 'where clause'
kevinresol commented 4 years ago

I think you can start by debugging these two places, checking if the table name/alias is correct:

https://github.com/haxetink/tink_sql/blob/097c652f7be854385e933730c17f7f376595156f/src/tink/sql/format/SqlFormatter.hx#L353

https://github.com/haxetink/tink_sql/blob/524ca1122229f7d3a24fc76a044ade3fe5b6cb53/src/tink/sql/macros/TableBuilder.hx#L236