Embarking on my Nest JS journey, I set up my first project to familiarize myself with it. Despite successfully working with the Organization entity, I encountered a roadblock when trying to create a User - organizationId IS NULL and cannot be saved.
Here are my entities:
import { User } from "src/modules/user/entities/user.entity";
import { IOrganization } from "../interfaces/organization.interface";
import { Column, CreateDateColumn, Entity, PrimaryColumn, UpdateDateColumn, OneToMany } from "typeorm";
@Entity()
export class Organization implements IOrganization {
@PrimaryColumn()
id: string
@Column({ nullable: false })
name: string
@OneToMany(() => User, (user) => user.organization)
users: User[]
@CreateDateColumn()
createdAt: Date
@UpdateDateColumn()
updatedAt: Date
}
import { Organization } from "src/modules/organization/entities/organization.entity";
import { Column, CreateDateColumn, Entity, PrimaryColumn, UpdateDateColumn, ManyToOne, JoinColumn } from "typeorm";
import { Role } from "../enums/user.enum";
@Entity()
export class User {
@PrimaryColumn()
id: string
@ManyToOne(() => Organization, (organization) => organization.users, {
nullable: false,
})
@JoinColumn({ name: "organizationId" })
organization: Organization
@Column({ unique: true, nullable: false })
email: string
@Column ({
type: "enum",
enum: Role,
default: Role.USER,
nullable: false,
})
role: Role
@Column({ nullable: false })
name: string
@CreateDateColumn()
createdAt: Date
@UpdateDateColumn()
updatedAt: Date
}
Moving on to my User Module:
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserService } from './user.service';
import { UserController } from './user.controller';
import { User } from './entities/user.entity';
import { OrganizationService } from '../organization/organization.service';
@Module({
imports: [TypeOrmModule.forFeature([User])],
controllers: [UserController],
providers: [UserService],
})
export class UserModule {}
Followed by my User Controller:
import { UserService } from './user.service';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { Roles } from 'src/decorators/roles.decorator';
import { AuthGuard } from '@nestjs/passport';
@Controller('user')
export class UserController {
constructor(private readonly userService: UserService) {}
// Rest of the controller methods...
}
And wrapping it up with my User Service:
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { IUser } from './interfaces/user.interface';
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private userRepository: Repository<User>,
) {}
// Rest of the service methods...
}
The issue arises during creation where the organizationId ends up as NULL in the database, triggering a violation of the not-null constraint. I am unsure why the data passed does not include organizationId. Seeking assistance for this seemingly simple problem, I attempted various solutions like importing the organization modules and modifying the repository import, but to no avail.