Skip to content

Why is ID implicitly included in Select queries ? #3029

Answered by a8m
jiaxuan asked this question in Q&A
Discussion options

You must be logged in to vote

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:

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

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
0 replies
Answer selected by jiaxuan
Comment options

You must be logged in to vote
1 reply
@a8m
Comment options

Comment options

You must be logged in to vote
1 reply
@a8m
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants