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] [Prisma] Multiple enums on model not correctly imported #2939

Closed
Tomdrouv1 opened this issue Dec 12, 2024 · 2 comments · Fixed by #2941
Closed

[BUG] [Prisma] Multiple enums on model not correctly imported #2939

Tomdrouv1 opened this issue Dec 12, 2024 · 2 comments · Fixed by #2941
Assignees

Comments

@Tomdrouv1
Copy link

Describe the bug

Hello @Romakita,

I noticed an issue with enums in schema.prisma, more precisely about their imports generated by the prisma generate command.

If you add multiple enums in your model like this :

schema.prisma

model User {
  id       Int      @id @default(autoincrement())
  email    String   @unique
  name     String?
  role     Role     @default(USER)
  provider Provider @default(EMAIL)
}

enum Role {
  USER
  ADMIN
}

enum Provider {
  EMAIL
  FACEBOOK
  GOOGLE
  GITHUB
  MICROSOFT
}

UserModel looks like this:

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var _a;
import { Integer, Required, Property, Allow, Enum } from "@tsed/schema";
import { Provider } from "../enums/index.js";
export class UserModel {
}
__decorate([
    Property(Number),
    Integer(),
    Required(),
    __metadata("design:type", Number)
], UserModel.prototype, "id", void 0);
__decorate([
    Property(String),
    Required(),
    __metadata("design:type", String)
], UserModel.prototype, "email", void 0);
__decorate([
    Property(String),
    Allow(null),
    __metadata("design:type", String)
], UserModel.prototype, "name", void 0);
__decorate([
    Required(),
    Enum(Role),
    __metadata("design:type", typeof (_a = typeof Role !== "undefined" && Role) === "function" ? _a : Object)
], UserModel.prototype, "role", void 0);
__decorate([
    Required(),
    Enum(Provider),
    __metadata("design:type", String)
], UserModel.prototype, "provider", void 0);

You can see that Role is not imported from './enums/index.js' but it is correctly generated in the enums folder.

To Reproduce

  1. Add multiple enums in schema.prisma
  2. Connect them to a model
  3. Generate schemas
  4. Import the model
  5. Start the server

Expected behavior

The generator should be able to import all enums defined in a model :

var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var _a;
import { Integer, Required, Property, Allow, Enum } from "@tsed/schema";
import { Provider, Role } from "../enums/index.js";
export class UserModel {
}
__decorate([
    Property(Number),
    Integer(),
    Required(),
    __metadata("design:type", Number)
], UserModel.prototype, "id", void 0);
__decorate([
    Property(String),
    Required(),
    __metadata("design:type", String)
], UserModel.prototype, "email", void 0);
__decorate([
    Property(String),
    Allow(null),
    __metadata("design:type", String)
], UserModel.prototype, "name", void 0);
__decorate([
    Required(),
    Enum(Role),
    __metadata("design:type", typeof (_a = typeof Role !== "undefined" && Role) === "function" ? _a : Object)
], UserModel.prototype, "role", void 0);
__decorate([
    Required(),
    Enum(Provider),
    __metadata("design:type", String)
], UserModel.prototype, "provider", void 0);

Code snippets

No response

Repository URL example

https://codesandbox.io/p/devbox/8mxt56

OS

Windows

Node version

Node v22.5.1

Library version

v8.3.3

Additional context

No response

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 8.3.5 🎉

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
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants