sqlboiler icon indicating copy to clipboard operation
sqlboiler copied to clipboard

Incorrect query builder syntax for TOP + DISTINCT on MS SQL

Open severedsea opened this issue 5 years ago • 0 comments

If you're having a generation problem please answer these questions before submitting your issue. Thanks!

What version of SQLBoiler are you using (sqlboiler --version)?

v4.2.0

What is your database and version (eg. Postgresql 10)

Microsoft SQL Server 2019

If this happened at generation time what was the full SQLBoiler command you used to generate your models? (if not applicable leave blank)

If this happened at runtime what code produced the issue? (if not applicable leave blank)

results, err := models.SomeTables(
	qm.Distinct(models.TableNames.SomeTable+".*"),
	qm.Limit(10),
).All(ctx, tx)

What is the output of the command above with the -d flag added to it? (Provided you are comfortable sharing this, it contains a blueprint of your schema)

SELECT  TOP (10) DISTINCT some_table.* FROM some_table;
mssql: Incorrect syntax near the keyword 'DISTINCT'.
        	            	bind failed to execute query

Please provide a relevant database schema so we can replicate your issue (Provided you are comfortable sharing this)

Further information. What did you do, what did you expect?

The query builders construct query by adding the TOP statement before the DISTINCT (https://github.com/volatiletech/sqlboiler/blob/master/queries/query_builders.go#L53-L76)

Valid syntax (at least on MS SQL) is SELECT DISTINCT TOP (10) ... https://stackoverflow.com/a/37642799

Note: Reason why there's a DISTINCT is because I have joins for conditional statements on the query.

severedsea avatar Nov 05 '20 16:11 severedsea