You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I got Cannot access 'User' before initialization because of a relation? Circular import, or what?
A relation is pretty basic in sql, how to solve this?
schema.prisma:
generator client {
provider = "prisma-client-js"
}
generator prismaClassGenerator {
provider = "prisma-class-generator"
output = "../src/_gen/prisma-class"
dryRun = "false"
// separateRelationFields = "false"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean? @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
Generated output:
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { User } from './user';
export class Post {
@ApiProperty({ type: Number })
id: number;
@ApiProperty({ type: String })
title: string;
@ApiPropertyOptional({ type: String })
content?: string;
@ApiPropertyOptional({ type: Boolean })
published?: boolean;
@ApiPropertyOptional({ type: () => User })
author?: User;
@ApiPropertyOptional({ type: Number })
authorId?: number;
}
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Post } from './post';
export class User {
@ApiProperty({ type: Number })
id: number;
@ApiProperty({ type: String })
email: string;
@ApiPropertyOptional({ type: String })
name?: string;
@ApiProperty({ isArray: true, type: () => Post })
posts: Post[];
}
The text was updated successfully, but these errors were encountered:
I got Cannot access 'User' before initialization because of a relation? Circular import, or what?
A relation is pretty basic in sql, how to solve this?
schema.prisma:
Generated output:
The text was updated successfully, but these errors were encountered: