-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Preserve custom type of auto-generated id property #4270
Preserve custom type of auto-generated id property #4270
Conversation
Can someone help me with the test? It seems not to be happening correctly |
The changes looks reasonable to me and local unit test works successfully. TASK=test-repository-mongodb and TASK=test-repository-mysql fail in travis ci.
Type |
I certainly did not know that I took code from outside the repository to test, thanks for your help. |
packages/repository/src/model.ts
Outdated
@@ -106,6 +106,11 @@ export class ModelDefinition { | |||
const definition = (definitionOrType as PropertyDefinition).type | |||
? (definitionOrType as PropertyDefinition) | |||
: {type: definitionOrType}; | |||
|
|||
if (definition.id === true && definition.generated === true) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be safe, I am proposing to extend this check to apply useDefaultIdType: false
only when an explicit type
is provided.
This property should set useDefaultIdType: false
:
@property({
type: 'string',
id: true,
generated: true
})
id: string;
This property should NOT set useDefaultIdType: false
:
@property({
id: true,
generated: true
})
id: string | number;
Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it seems right to me. I just implemented it
Thank you @frbuceta for the pull request, this is great!
Yeah, this is a problem we have recently discovered too, see #2398. I think we need to fix our connectors to correctly support auto-generated string ids (primary keys) before we can move forward with this pull request. |
On the second thought, I think this is a bug in the way how our shared test suite is creating models. The way how this test suite is supposed to work, is that the connector specifies what type is used for primary keys (number vs. string) and the test suite honors that type when defining new models. I suspect the code building AFAICT, Here is the correct definition of the PK property in a different model: I think we need to add |
I am working on Postgres: allow string type attribute to be auto-generated . That PR is proposing to use
I've tested it locally, this solution works in Once it's done, we can rework the MySQL connector with a similar solution. Please share your idea there if you have any 😄 |
What if someone wants to have a different generator function? (e.g. different UUID version or database extension, different function name) |
That's doable. It will be user's responsibility to provide valid extension and defaultFn. |
I've tried this but it doesn't work. Right now with the only one I have problems with is mongo. |
packages/repository/src/model.ts
Outdated
if ( | ||
definition.id === true && | ||
definition.generated === true && | ||
definition.type === 'string' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am afraid we misunderstood each other. Here is what I meant:
definition.type === 'string' | |
definition.type !== undefined |
Either way, please add a new test to verify that useDefaultIdType
is not set when definition.type
was not provided (and was not inferred from TypeScript metadata either).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is no model in the tests of the type as undefined, how should I generate a model with these features?
Hmm... @agnes512 IIRC, you were dealing with similar issues recently, while working on inclusion resolvers. Can you please help @frbuceta to find a way how to make repository tests pass against MongoDB? |
Thanks for your help @agnes512 |
}); | ||
const order = await orderRepo.create({ | ||
description: 'an order', | ||
customerId: parent.id, | ||
}); | ||
|
||
const result = await customerRepo.find({ | ||
include: [{relation: 'orders'}, {relation: 'customers'}], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While I was fixing the CI failure, I think the flag might somehow break how we deal with ObjectId
in MongoDB. Before we were able to traverse related instances with ObjectId
. Not sure it has something to do with the type.
Implements #3602
Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated👉 Check out how to submit a PR 👈