Skip to content
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: do not rely on model loading order to find an implicit owner #10360

Merged
merged 7 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/cold-dragons-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/utils": patch
---

fix: do not rely on model loading order to find an implicit owner
99 changes: 61 additions & 38 deletions packages/core/utils/src/dml/__tests__/entity-builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5149,9 +5149,9 @@ describe("Entity builder", () => {
reference: "m:n",
name: "teams",
entity: "Team",
owner: true,
owner: false,
pivotTable: "team_users",
inversedBy: "users",
mappedBy: "users",
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -5217,9 +5217,9 @@ describe("Entity builder", () => {
users: {
reference: "m:n",
name: "users",
mappedBy: "teams",
inversedBy: "teams",
entity: "User",
owner: false,
owner: true,
pivotTable: "team_users",
},
created_at: {
Expand Down Expand Up @@ -5330,9 +5330,9 @@ describe("Entity builder", () => {
reference: "m:n",
name: "teams",
entity: "Team",
owner: true,
owner: false,
pivotTable: "team_users",
inversedBy: "users",
mappedBy: "users",
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -5399,8 +5399,8 @@ describe("Entity builder", () => {
reference: "m:n",
name: "users",
entity: "User",
owner: false,
mappedBy: "teams",
owner: true,
inversedBy: "teams",
pivotTable: "team_users",
},
created_at: {
Expand Down Expand Up @@ -5545,9 +5545,9 @@ describe("Entity builder", () => {
reference: "m:n",
name: "teams",
entity: "Team",
owner: true,
owner: false,
pivotTable: "team_users",
inversedBy: "users",
mappedBy: "users",
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -5614,9 +5614,9 @@ describe("Entity builder", () => {
reference: "m:n",
name: "users",
entity: "User",
owner: false,
owner: true,
pivotTable: "team_users",
mappedBy: "teams",
inversedBy: "teams",
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -6125,9 +6125,9 @@ describe("Entity builder", () => {
reference: "m:n",
name: "teams",
entity: "Team",
owner: true,
owner: false,
pivotTable: "platform.team_users",
inversedBy: "users",
mappedBy: "users",
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -6195,8 +6195,8 @@ describe("Entity builder", () => {
reference: "m:n",
name: "users",
entity: "User",
owner: false,
mappedBy: "teams",
owner: true,
inversedBy: "teams",
pivotTable: "platform.team_users",
},
created_at: {
Expand Down Expand Up @@ -6719,10 +6719,10 @@ describe("Entity builder", () => {
},
})

const metaData = MetadataStorage.getMetadataFromDecorator(User)
expect(metaData.className).toEqual("User")
expect(metaData.path).toEqual("User")
expect(metaData.properties).toEqual({
const teamMetaData = MetadataStorage.getMetadataFromDecorator(Team)
expect(teamMetaData.className).toEqual("Team")
expect(teamMetaData.path).toEqual("Team")
expect(teamMetaData.properties).toEqual({
id: {
reference: "scalar",
type: "number",
Expand All @@ -6733,23 +6733,23 @@ describe("Entity builder", () => {
getter: false,
setter: false,
},
username: {
name: {
reference: "scalar",
type: "string",
columnType: "text",
name: "username",
fieldName: "username",
name: "name",
fieldName: "name",
nullable: false,
getter: false,
setter: false,
},
teams: {
users: {
reference: "m:n",
name: "teams",
entity: "Team",
name: "users",
entity: "User",
owner: true,
inversedBy: "teams",
pivotEntity: "TeamUsers",
inversedBy: "users",
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -6788,10 +6788,10 @@ describe("Entity builder", () => {
},
})

const teamMetaData = MetadataStorage.getMetadataFromDecorator(Team)
expect(teamMetaData.className).toEqual("Team")
expect(teamMetaData.path).toEqual("Team")
expect(teamMetaData.properties).toEqual({
const metaData = MetadataStorage.getMetadataFromDecorator(User)
expect(metaData.className).toEqual("User")
expect(metaData.path).toEqual("User")
expect(metaData.properties).toEqual({
id: {
reference: "scalar",
type: "number",
Expand All @@ -6802,23 +6802,23 @@ describe("Entity builder", () => {
getter: false,
setter: false,
},
name: {
username: {
reference: "scalar",
type: "string",
columnType: "text",
name: "name",
fieldName: "name",
name: "username",
fieldName: "username",
nullable: false,
getter: false,
setter: false,
},
users: {
teams: {
reference: "m:n",
name: "users",
entity: "User",
name: "teams",
entity: "Team",
owner: false,
mappedBy: "teams",
pivotEntity: "TeamUsers",
mappedBy: "users",
},
created_at: {
reference: "scalar",
Expand Down Expand Up @@ -6857,5 +6857,28 @@ describe("Entity builder", () => {
},
})
})

test("throw error when both sides of relationship defines the pivot table", () => {
const team = model.define("team", {
id: model.number(),
name: model.text(),
users: model.manyToMany(() => user, {
pivotTable: "user_teams",
}),
})

const user = model.define("user", {
id: model.number(),
username: model.text(),
teams: model.manyToMany(() => team, {
pivotTable: "team_users",
mappedBy: "users",
}),
})

expect(() => toMikroORMEntity(user)).toThrow(
`Invalid relationship reference for "User.teams". Define "pivotTable", "joinColumn", or "inverseJoinColumn" on only one side of the relationship`
)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import {
Property,
rel,
} from "@mikro-orm/core"
import { camelToSnakeCase, pluralize } from "../../../common"
import { DmlEntity } from "../../entity"
import { HasMany } from "../../relations/has-many"
import { HasOne } from "../../relations/has-one"
import { ManyToMany as DmlManyToMany } from "../../relations/many-to-many"
import { applyEntityIndexes } from "../mikro-orm/apply-indexes"
import { HasMany } from "../../relations/has-many"
import { parseEntityName } from "./parse-entity-name"
import { camelToSnakeCase, pluralize } from "../../../common"
import { applyEntityIndexes } from "../mikro-orm/apply-indexes"
import { ManyToMany as DmlManyToMany } from "../../relations/many-to-many"

type Context = {
MANY_TO_MANY_TRACKED_RELATIONS: Record<string, boolean>
Expand Down Expand Up @@ -375,12 +375,10 @@ export function defineManyToManyRelationship(
>,
{
relatedModelName,
relatedTableName,
pgSchema,
}: {
relatedModelName: string
pgSchema: string | undefined
relatedTableName: string
},
{ MANY_TO_MANY_TRACKED_RELATIONS }: Context
) {
Expand Down Expand Up @@ -450,11 +448,12 @@ export function defineManyToManyRelationship(
pivotEntityName = parseEntityName(pivotEntity).modelName
}

if (!pivotEntityName) {
const { tableName } = parseEntityName(entity)
let tableNameWithoutSchema: string
let relatedTableNameWithoutSchema: string
const tableName = parseEntityName(entity).tableNameWithoutSchema
const relatedTableName = parseEntityName(relatedEntity).tableNameWithoutSchema
const sortedTableNames = [tableName, relatedTableName].sort()
const otherSideRelationOptions = otherSideRelationship.parse("").options

if (!pivotEntityName) {
/**
* Pivot table name is created as follows (when not explicitly provided)
*
Expand All @@ -464,25 +463,10 @@ export function defineManyToManyRelationship(
* - And finally pluralizing the second entity name.
*/

let [schema, ...tableTokens] = tableName.split(".")
if (!tableTokens.length) {
tableNameWithoutSchema = schema
} else {
tableNameWithoutSchema = tableTokens.join(".")
}

const [relatedSchema, ...relatedTableTokens] = relatedTableName.split(".")
if (!relatedTableTokens.length) {
relatedTableNameWithoutSchema = relatedSchema
} else {
relatedTableNameWithoutSchema = relatedTableTokens.join(".")
}

pivotTableName =
relationship.options.pivotTable ??
otherSideRelationship.parse("").options.pivotTable ??
[tableNameWithoutSchema, relatedTableNameWithoutSchema]
.sort()
sortedTableNames
.map((token, index) => {
if (index === 1) {
return pluralize(token)
Expand All @@ -492,22 +476,51 @@ export function defineManyToManyRelationship(
.join("_")
}

const otherSideRelationOptions = otherSideRelationship.parse("").options
let isOwner: boolean | undefined = undefined

const isOwner =
!!joinColumn ||
!!inverseJoinColumn ||
!!relationship.options.pivotTable ||
/**
* We can't infer it from the current entity so lets
* look at the otherside configuration as well to make a choice
*/
(!otherSideRelationOptions.pivotTable &&
!otherSideRelationOptions.joinColumn &&
!otherSideRelationOptions.inverseJoinColumn &&
!MANY_TO_MANY_TRACKED_RELATIONS[
`${relatedModelName}.${otherSideRelationshipProperty}`
])
const configuresRelationship = !!(
joinColumn ||
inverseJoinColumn ||
relationship.options.pivotTable
)
const relatedOneConfiguresRelationship = !!(
otherSideRelationOptions.pivotTable ||
otherSideRelationOptions.joinColumn ||
otherSideRelationOptions.inverseJoinColumn
)

/**
* Both sides are configuring the properties that must be on one
* side only
*/
if (configuresRelationship && relatedOneConfiguresRelationship) {
throw new Error(
`Invalid relationship reference for "${MikroORMEntity.name}.${relationship.name}". Define "pivotTable", "joinColumn", or "inverseJoinColumn" on only one side of the relationship`
)
}

/**
* If any of the following properties are provided, we consider
* the current side to be the owner
*/
if (configuresRelationship) {
isOwner = true
}

/**
* If any of the properties are provided on the other side,
* then we do not expect the current side to be the owner
*/
if (isOwner === undefined && relatedOneConfiguresRelationship) {
isOwner = false
}

/**
* Finally, we consider the current side as owner, if it is
* the first one in alphabetical order. The same logic is
* applied to pivot table name as well.
*/
isOwner ??= sortedTableNames[0] === tableName

const mappedByProp = isOwner ? "inversedBy" : "mappedBy"
const mappedByPropValue =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ export function parseEntityName(entity: DmlEntity<any, any>) {
tableName,
modelName: upperCaseFirst(toCamelCase(parsedEntity.name)),
pgSchema: rest.length ? pgSchema : undefined,
tableNameWithoutSchema: rest.length ? rest.join(".") : pgSchema,
}
}
Loading
Loading