-
-
Notifications
You must be signed in to change notification settings - Fork 496
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
loadItems
fails with invalid field name exception
#5191
Comments
Just a side note, as I see this way too often: you don't need that problematic |
I think it all boils down to personal preference. In our case we converted a large code base from typeorm to mikro-orm and therefore we need the relation id inside the entity to keep the change set as small as possible. Especially on the frontend, where we use relation ids a lot. Also for our team it feels more common to access the relation id instead of the relation directly. I think there is no benefit from one solution over the other, it's just nice if a ORM supports both patterns. While we talk about relation ids: We really like the |
Also, your repro seems wrong, you are setting @ManyToOne(() => User, { fieldName: 'user_id' })
user!: User; And in fact this also does not need to be there, this is the default mapping already.
I see, so the motivation is to have the |
The point of I don't mind opt-in improvements, so PR welcome if you see a need for it, it just feels weird. |
Back to your repro, as I mentioned, I believe its just a misusage, as you define two properties for the FK and they dont have matching column names. If the column is supposed to be called @ManyToOne(() => User)
user!: User;
@Property({ persist: false })
userId!: number & Opt; Alternatively, if you want it to be @ManyToOne(() => User, { fieldName: 'userId' })
user!: User;
@Property({ persist: false, fieldName: 'userId' })
userId!: number & Opt; |
It is a bit hard for me to create a nice reproducible case here. I tried to show you that the alias is missing in some cases. This indicates a larger issue for me. Now we saw similar issues with the find method. Something like the following throws an
If I print the sql statement I see that the alias is missing for the key defined in the fields array. If you need a full reproducible case, I'm happy to construct one.
You are right, I did a mistake in this repo, but it should show that the alias is missing. Can you confirm this? Otherwise I have to try to fix the repo.
Yes, but also if I do not populate the relation. I always need it, especially on the frontend.
I think it's better to discuss this in another thread. |
It's missing on purpose on virtual properties. Those are not added to the select clause by the ORM, but due to the misconfigured virtual column, you end up with it selected - because the
This is surely not as simple as you think, partial loading works for nested entities and requires dot paths. So yes, provide a complete repro. |
In our production code, the field is not misconfigured and we have the same issue. I do not really understand why virtual properties are added to the select and why you are saying that they are added on purpose without alias. What's the idea behind this?
I looked at the code and I can guarantee you that I don't have wrong assumptions on that. The code is awesome and you put a lot of effort into it. Fixing such things is obviously not easy and I appreciate every help. I will try to add a case to the repo, but probably not today. I send you a message if I'm done. |
That's the thing, they are not added to the select by the ORM, in your repro this happens because of the misconfiguration I pointed out. They should not have alias, because they are not supposed to be representing a real column - the way you use them for the FKs was always just a workaround. The use case for those is something you compute and alias. https://mikro-orm.io/docs/defining-entities#virtual-properties Maybe worth noting that there is a way to enforce alias on virtual property - use a formula instead. |
Describe the bug
During a call to
loadItems
I get anInvalidFieldNameException
with eitherUnknown column 'xxxx' in 'field list'
orColumn 'xxxx' in field list is ambiguous
.What I noticed is that in both cases there is a property with the lazy attribute, so the field list gets expanded. So instead of
b0.*
, all attributes are listed, but the field names from relations are without alias. E.g.@ManyToOne(() => User, { fieldName: "userId" })
in this case the field list would contain something like..., b0.foo, userId, b0.bar, ...
Reproduction
https://github.com/sualko/reproduction/tree/load-items
I created an example which shows that not all fields in the selection list are aliased, even if the example does not exactly print the error message from above.
If you set the lazy attribute to false in the example, everything works.
What driver are you using?
@mikro-orm/mysql
MikroORM version
6.0.7
Node.js version
18.13.0
Operating system
Ubuntu 22.04
Validations
The text was updated successfully, but these errors were encountered: