-
I followed the examples to define a simple schema:
However, this simple query produces unexpected result:
The underlying query turns out to be I'm able to achieve similar effect with The testing is done with ent v0.11.3 and sqlite3. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hey @jiaxuan 👋 That's a good question, and the reason is that pets, err := client.Pet.
Query().
Select(pet.FieldName).
All(context.Background())
// Update a pet.
pets[0].Update().SetName(1.1).Save(ctx)
// Query associations.
pets[1].QueryOwner().QueryFriends().All(ctx)
// Delete a pet.
client.Pet.DelelteOne(pets[2]) For all the commands above (and there are more), the ID field is necessary. However, if you just want to select 2 columns without returning Ent nodes/objects, Ent won't load the ID field in this case. names, err := client.Pet.Query().
Select(pet.FieldName).
Strings(ctx)
var v []struct {
Name string
Age float64
}
err := client.Pet.Query().
Select(pet.FieldName, pet.FieldAge).
Scan(ctx, &v) I hope it helped. |
Beta Was this translation helpful? Give feedback.
-
Hi Ariel, Thanks for the answer! That worked! I actually tried this before asking the question here and it didn't get me the result I wanted. The generated query doesn't have What tripped me up was I assumed |
Beta Was this translation helpful? Give feedback.
-
This is where I got that false impression from when I was trying to come up with a custom predicate: https://entgo.io/docs/predicates Most examples have |
Beta Was this translation helpful? Give feedback.
Hey @jiaxuan 👋
That's a good question, and the reason is that
All
is not a simple select, as it returns Ent nodes/objects that later can be used as follows:For all the commands above (and there are more), the ID field is necessary.
However, if you just want to select 2 columns without returning Ent nodes/objects, Ent won't load the ID field in this case.