-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Fix bug with new nested where filters #8005
Changes from all commits
cd543dc
b56813c
6de9327
a2602e5
6c509c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -993,51 +993,68 @@ export abstract class QueryBuilder<Entity> { | |
|
||
if (metadata.hasRelationWithPropertyPath(path)) { | ||
const relation = metadata.findRelationWithPropertyPath(path)!; | ||
const isEntityTarget = relation.inverseEntityMetadata.target === entity[key].constructor; | ||
|
||
// There's also cases where we don't want to return back all of the properties. | ||
// These handles the situation where someone passes the model & we don't need to make | ||
// a HUGE `where` to uniquely look up the entity. | ||
const primaryColumns = relation.inverseEntityMetadata.primaryColumns; | ||
const seenPrimaryColumns = new Set<string>(); | ||
for (const primaryColumn of primaryColumns) { | ||
const entityValueMap = primaryColumn.getEntityValueMap(entity[key]); | ||
for (const [columnName, value] of Object.entries(entityValueMap || {})) { | ||
if (value) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is |
||
seenPrimaryColumns.add(columnName); | ||
} else { | ||
break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we know this inner one is invalid, we know they all are. Do we want to |
||
} | ||
} | ||
} | ||
const hasOnlyPrimaryKeys = primaryColumns.length > 0 && seenPrimaryColumns.size === Object.keys(entity[key]).length; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If it's only primary columns do we actually care if it hits the optimizations? In theory, it would be the same either way, wouldn't it? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The update flow uses this method and fails if you pass |
||
|
||
if (isEntityTarget || hasOnlyPrimaryKeys) { | ||
// There's also cases where we don't want to return back all of the properties. | ||
// These handles the situation where someone passes the model & we don't need to make | ||
// a HUGE `where` to uniquely look up the entity. | ||
|
||
// In the case of a *-to-one, there's only ever one possible entity on the other side | ||
// so if the join columns are all defined we can return just the relation itself | ||
// because it will fetch only the join columns and do the lookup. | ||
if (relation.relationType === "one-to-one" || relation.relationType === "many-to-one") { | ||
const joinColumns = relation.joinColumns | ||
.map(j => j.referencedColumn) | ||
.filter((j): j is ColumnMetadata => !!j); | ||
|
||
const hasAllJoinColumns = joinColumns.length > 0 && joinColumns.every( | ||
column => column.getEntityValue(entity[key], false) | ||
); | ||
|
||
if (hasAllJoinColumns) { | ||
paths.push(path); | ||
continue; | ||
} | ||
} | ||
|
||
Comment on lines
+1017
to
+1034
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you're right and I can have this block only apply to isEntityTarget. |
||
if (relation.relationType === "one-to-many" || relation.relationType === "many-to-many") { | ||
throw new Error(`Cannot query across ${relation.relationType} for property ${path}`); | ||
} | ||
Comment on lines
+1035
to
+1037
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pretty sure this needs to remain outside the |
||
|
||
// In the case of a *-to-one, there's only ever one possible entity on the other side | ||
// so if the join columns are all defined we can return just the relation itself | ||
// because it will fetch only the join columns and do the lookup. | ||
if (relation.relationType === "one-to-one" || relation.relationType === "many-to-one") { | ||
const joinColumns = relation.joinColumns | ||
.map(j => j.referencedColumn) | ||
.filter((j): j is ColumnMetadata => !!j); | ||
// For any other case, if the `entity[key]` contains all of the primary keys we can do a | ||
// lookup via these. We don't need to look up via any other values 'cause these are | ||
// the unique primary keys. | ||
// This handles the situation where someone passes the model & we don't need to make | ||
// a HUGE where. | ||
|
||
const hasAllJoinColumns = joinColumns.length > 0 && joinColumns.every( | ||
const hasAllPrimaryKeys = primaryColumns.length > 0 && primaryColumns.every( | ||
column => column.getEntityValue(entity[key], false) | ||
); | ||
|
||
if (hasAllJoinColumns) { | ||
paths.push(path); | ||
if (hasAllPrimaryKeys) { | ||
const subPaths = primaryColumns.map( | ||
column => `${path}.${column.propertyPath}` | ||
); | ||
paths.push(...subPaths); | ||
continue; | ||
} | ||
} | ||
|
||
if (relation.relationType === "one-to-many" || relation.relationType === "many-to-many") { | ||
throw new Error(`Cannot query across ${relation.relationType} for property ${path}`); | ||
} | ||
|
||
// For any other case, if the `entity[key]` contains all of the primary keys we can do a | ||
// lookup via these. We don't need to look up via any other values 'cause these are | ||
// the unique primary keys. | ||
// This handles the situation where someone passes the model & we don't need to make | ||
// a HUGE where. | ||
const primaryColumns = relation.inverseEntityMetadata.primaryColumns; | ||
const hasAllPrimaryKeys = primaryColumns.length > 0 && primaryColumns.every( | ||
column => column.getEntityValue(entity[key], false) | ||
); | ||
|
||
if (hasAllPrimaryKeys) { | ||
const subPaths = primaryColumns.map( | ||
column => `${path}.${column.propertyPath}` | ||
); | ||
paths.push(...subPaths); | ||
continue; | ||
} | ||
|
||
// If nothing else, just return every property that's being passed to us. | ||
const subPaths = this.createPropertyPath(relation.inverseEntityMetadata, entity[key]) | ||
.map(p => `${path}.${p}`); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ async function createDotenvFiles() { | |
} | ||
|
||
async function createYamlFiles() { | ||
await fs.mkdir(path.join(__dirname, "configs/yaml")); | ||
await fs.mkdir(path.join(__dirname, "configs/yaml"), { recursive: true }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this change needed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't able to run test-fast multiple times in a row. It would complain that the directory already existed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a bigger problem that our tests have than just this. Can you omit this change for now just to simplify this PR & what it touches? If we have to revert or find the cause of something it makes it a whole lot easier if it's one PR for one change. |
||
await fs.writeFile(path.join(__dirname, "configs/yaml/test-yaml.yaml"), "- type: \"sqlite\"\n name: \"file\"\n database: \"test-yaml\""); | ||
} | ||
|
||
|
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.
What about embedded entity primary keys? Is that a concern here?
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'm not sure - happy to hand off this pr to you if its easier.
Otherwise please point me to docs and I can try to add a test case.