ent icon indicating copy to clipboard operation
ent copied to clipboard

Query().Unique(true) includes fields not specified in Select

Open domust opened this issue 1 year ago • 1 comments

Uniqueness query includes an id field, which is not specified in the explicit call to select method.

Thank you very much for contributing to Ent by creating an issue! ❤️ To avoid duplicate issues we ask you to check off the following list. -->

  • [x] The issue is present in the latest release.
  • [x] I have searched the issues of this repository and believe that this is not a duplicate.

Current Behavior 😯

SELECT DISTINCT id, name
FROM persons

Expected Behavior 🤔

SELECT DISTINCT name
FROM persons

Steps to Reproduce 🕹

Steps:

  1. Define a table:
import "entgo.io/ent"

type Person struct {
        ent.Schema
}
  1. Define the columns:
import (
        "entgo.io/ent"
        "entgo.io/ent/schema/field"
        "github.com/google/uuid"
)

func (Person) Fields() []ent.Field {
        return []ent.Fields{
                field.UUID("id", uuid.UUID{}).Default(uuid.New),
                field.String("name"),
        }
}
  1. Insert 2 persons with the same name.

  2. Execute the following query:

people, err := entClient.Person.Query().Unique(true).Select(person.FieldName).All(ctx)
// len(people) should return 1, not 2.

Your Environment 🌎

Tech Version
Go 1.22.?
Ent 0.12.3
Database PostgreSQL
Driver https://github.com/lib/pq

domust avatar Jun 12 '24 12:06 domust

What about using Scan instead?

godcong avatar Jun 18 '24 17:06 godcong