Skip to content

.One() does not add a limit - resulting in unexpectedly large queries #579

@barneyferry

Description

@barneyferry

Noticed this while investigating an increasingly large query to get the latest "version" of a resource, query.One does not seem to impose a limit at all, it simply calls

	sql, args, err := Build(ctx, q)
	if err != nil {
		return t, err
	}

	if l, ok := q.(MapperModder); ok {
		if loaders := l.GetMapperMods(); len(loaders) > 0 {
			m = scan.Mod(m, loaders...)
		}
	}

	t, err = scan.One(ctx, exec, m, sql, args...)
	if err != nil {
		return t, err
	}

meaning every .One call actually needs:

	query.Expression.Limit.SetLimit(1)

	r, err := query.One(ctx, q.db)

which is quite misleading compared to other ORMs I've used. The "One" implies it will query one row, not query all rows and only return one.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions