Closed mcastrup closed 5 months ago
The reason for this duplication is in your definition here:
The Status association results in Status_code
field.
'Confirmed' as Status_code : String,
is redundant here.
I think the issue is with your definition, not cds-typer
. Status_code
should be defined automatically from the association, not sure why it's not for you.
Hi Michael,
thank you for the detailed report, and thank you, Tomas, for looking into the issue! I agree with Tomas' observation. Still, I also acknowledge the potential for confusion if manually defined fields clash with ones that have been generated automatically -- regardless of the reason.
I will therefore address this issue by raising an error and not generating the foreign key field in that case.
Best, Daniel
Hi @daogrady ,
I have still two questions regarding your and Tomas' answers:
Why do you include the key field of the associated entity into the generated artefacts, i.e. in this case the code
property of OutboxedTransactionStatusCodes
as Status_code
?
We are trying to replace the module cds2types by your cds-typer. As I already mentioned, the type, which is generated by cds2types, includes only the Status
property from the association. The property Status_code
in the generated type is related to the explicitly defined property in the entity.
I also played around and tried several options to refactor the entity. However, I think there is a reason for the comment in the code:
'Confirmed' as Status_code : String, // this field needs to be defined explicitly, as it is not generated by the association
I think cds-typer should be able to handle such field clashes regardless of the reason, as you also mentioned.
You mention to raise an error and not to generate the foreign key field in such a case. Does this mean, the whole generation of all the types will break? Or dou you just ignore the duplicate property and everything else will work fine?
Best regards, Michael
Hi Michael,
A
to an entity B
, where B
has fields that are declared as key
, you can reference these fields via an underscore. This is apparently called "foreign key propagation" and I suppose it is done to avoid loading endless association chains when you just need the top level entity. This feature goes back to cds-typer v0.8, as it had been requested several times internally and here on GitHub. You can see one such issue here. I think cds-typer should be able to handle such field clashes regardless of the reason, as you also mentioned.
I am not sure how such a handling would look like. The workaround I will employ now is to not generate a foreign key field if the entity in question owns a property of the exact name the FK would have. This will avoid clashes. But it will inevitably lead to problems when a user tries to use such a field with the foreign key semantic, when in fact it is just a property that happens to have the same name. Therefore the error message.
Best, Daniel
Is there an existing issue for this?
Nature of Your Project
TypeScript
Current Behavior
We defiend an entity
Transactions
in our model:The whole file is available here.
When running cds-typer, the following code is created in the index.ts file;
The property
Status_code
appears twice, which leads to the error: "Duplicate identifier 'Status_code'."Expected Behavior
I expect the last property
Status_code
not to be generated, like:When I compare the cds-typer generation with that of cds2types, I see the following result, created by cds2types:
Here, the
Status_code
appears only once.Steps To Reproduce
You can clone our repository crypto-for-business locally with
git clone https://github.tools.sap/erp4sme/crypto-for-business.git
, checkout the branchswitch_to_typer_prototype
and runnpm ci
andnpm run cds-typer:dev
Environment
Repository Containing a Minimal Reproducible Example
No response
Anything else?
There is already a similar issue: https://github.com/cap-js/cds-typer/issues/139 I think, it is only similar, but not the same.