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

[BUG] Optional forgein key cause ReferenceError in ESM mode #2863

Closed
vangie opened this issue Oct 16, 2024 · 10 comments · Fixed by #2864
Closed

[BUG] Optional forgein key cause ReferenceError in ESM mode #2863

vangie opened this issue Oct 16, 2024 · 10 comments · Fixed by #2864

Comments

@vangie
Copy link
Contributor

vangie commented Oct 16, 2024

Describe the bug

my project is run in ESM, I using Prisma create a optional forgein key relation between two Model,after npx prisma migrate, then throw following error on start up project.

file:///Users/duwan/Workspaces/babelcloud/babel-agent/node_modules/.pnpm/@[email protected]_@[email protected][email protected]__@[email protected]_@[email protected]_@t_vf7jy3uhtsnnr6bz5e5qbyw76e/node_modules/@tsed/prisma/lib/.schema/models/ConversationModel.js:119
    __metadata("design:type", AssignmentModel)
                              ^

ReferenceError: Cannot access 'AssignmentModel' before initialization
    at file:///Users/duwan/Workspaces/babelcloud/babel-agent/node_modules/.pnpm/@[email protected]_@[email protected][email protected]__@[email protected]_@[email protected]_@t_vf7jy3uhtsnnr6bz5e5qbyw76e/node_modules/@tsed/prisma/lib/.schema/models/ConversationModel.js:119:31
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

To Reproduce

model Conversation {
  id           String      @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  assignmentId String?     @map("assignment_id") @db.Uuid
  assignment   Assignment? @relation(fields: [assignmentId], references: [id], onDelete: NoAction, onUpdate: NoAction)

  @@map("conversation")
}

model Assignment {
  id                String         @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  conversations     Conversation[]

  @@map("assignment")
}

cause following error

file:///Users/duwan/Workspaces/babelcloud/babel-agent/node_modules/.pnpm/@[email protected]_@[email protected][email protected]__@[email protected]_@[email protected]_@t_vf7jy3uhtsnnr6bz5e5qbyw76e/node_modules/@tsed/prisma/lib/.schema/models/ConversationModel.js:119
    __metadata("design:type", AssignmentModel)
                              ^

ReferenceError: Cannot access 'AssignmentModel' before initialization
    at file:///Users/duwan/Workspaces/babelcloud/babel-agent/node_modules/.pnpm/@[email protected]_@[email protected][email protected]__@[email protected]_@[email protected]_@t_vf7jy3uhtsnnr6bz5e5qbyw76e/node_modules/@tsed/prisma/lib/.schema/models/ConversationModel.js:119:31
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

I found that if remove the '?' after String and Assignment as following, it will not cause the error.

model Conversation {

  assignmentId String     @map("assignment_id") @db.Uuid
  assignment   Assignment @relation(fields: [assignmentId], references: [id], onDelete: NoAction, onUpdate: NoAction)

}

Expected behavior

file:///Users/duwan/Workspaces/babelcloud/babel-agent/node_modules/.pnpm/@tSed+prisma@7.83.3_@prisma[email protected]_prisma@5.20.0__@tsed+core@7.83.3_@tsed+di@7.83.3_@t_vf7jy3uhtsnnr6bz5e5qbyw76e/node_modules/@tsed/prisma/lib/.schema/models/ConversationModel.js

...
__decorate([
    Property(() => AssignmentModel),
    Allow(null),
    __metadata("design:type", AssignmentModel)
], ConversationModel.prototype, "assignment", void 0);

if i change the secord AssignmentModel to Object manually, everything is well。

...
__decorate([
    Property(() => AssignmentModel),
    Allow(null),
    __metadata("design:type", Object)
], ConversationModel.prototype, "assignment", void 0);

Code snippets

No response

Repository URL example

No response

OS

macOS

Node version

v20.11.1

Library version

v7.83.3

Additional context

ESM mode

@Romakita
Copy link
Collaborator

Hi @vangie
Thanks for the issue.
Metadata are generated by typescript not by the plugin, so how I supposed to fix that? Have you a recent typescript version?

See you

@vangie
Copy link
Contributor Author

vangie commented Oct 16, 2024

Hi @vangie Thanks for the issue. Metadata are generated by typescript not by the plugin, so how I supposed to fix that? Have you a recent typescript version?

See you

@Romakita

❯ npx tsc --version
Version 5.5.3

@Romakita
Copy link
Collaborator

@vangie It seems to be solved by that:

  if (isRequired && !isList && !isNullable && hasCircularRef && ["inputObjectTypes", "enumTypes"].includes(location)) {
    hasCircularRef && !isList && field.model.addImportDeclaration("@tsed/core", "Relation", true);
    TSType = `Relation<${TSType}>`;
  }

But in this case, the code isn't generated as expected. You should have a Relation in the generated code.

See:

if (isRequired && !isList && !isNullable && hasCircularRef && ["inputObjectTypes", "enumTypes"].includes(location)) {

Maybe the condition isn't correct in your case.

@Romakita
Copy link
Collaborator

PR #2864

Copy link

🎉 Are you happy?

If you appreciated the support, know that it is free and is carried out on personal time ;)

A support, even a little bit makes a difference for me and continues to bring you answers!

github opencollective

1 similar comment
Copy link

🎉 Are you happy?

If you appreciated the support, know that it is free and is carried out on personal time ;)

A support, even a little bit makes a difference for me and continues to bring you answers!

github opencollective

@Romakita
Copy link
Collaborator

🎉 This issue has been resolved in version 7.83.4 🎉

The release is available on:

Your semantic-release bot 📦🚀

@vangie
Copy link
Contributor Author

vangie commented Oct 18, 2024

@Romakita My problem is resolved, Thanks!

@Romakita
Copy link
Collaborator

🎉 This issue has been resolved in version 8.0.0-beta.10 🎉

The release is available on:

Your semantic-release bot 📦🚀

@Romakita
Copy link
Collaborator

🎉 This issue has been resolved in version 8.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants