Skip to content

Cannot access 'User' before initialization #60

@n00el

Description

@n00el

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[];
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions