volatiletech / sqlboiler

Generate a Go ORM tailored to your database schema.
BSD 3-Clause "New" or "Revised" License
6.73k stars 544 forks source link

MSSQL: Exists() finisher should use COUNT(*) instead of non working COUNT([schema].[table].*) #392

Closed nekrondev closed 5 years ago

nekrondev commented 6 years ago

What version of SQLBoiler are you using?

3.0.0

If this happened at runtime what code produced the issue?

found, err := role.Permissions(qm.Where("active = ?", true)).Exists(d.ctx, d.db)

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

SQLBoiler will convert the above statement into:

SELECT  TOP (1) COUNT([dbo].[permission].*) 
FROM [dbo].[permission] 
INNER JOIN [dbo].[role_has_permission] on [dbo].[permission].[id] = [dbo].[role_has_permission].[permission_id] 
WHERE (active = 1) AND ([dbo].[role_has_permission].[role_id]=15)
MSSQL error: Incorrect syntax near '*'.

MSSQL uses COUNT(*) instead of providing a wildcard with the prefixed schema and table as SQLBoiler does.

Possible fix:

Remove selected coloumns by adding queries.SetSelect(q.Query, nil) to Exists() finisher template like you did for Count() finisher function.

File: 03_finishers.go.tpl

// Exists checks if the row exists in the table.
func (q {{$alias.DownSingular}}Query) Exists({{if .NoContext}}exec boil.Executor{{else}}ctx context.Context, exec boil.ContextExecutor{{end}}) (bool, error) {
    var count int64
    queries.SetCount(q.Query)
    queries.SetLimit(q.Query, 1)
        queries.SetSelect(q.Query, nil)
    {{if .NoContext -}}
    err := q.Query.QueryRow(exec).Scan(&count)
    {{else -}}`
aarondl commented 6 years ago

Fixed in dev branch.