'How can I create a primary Key (UUID) with a one to many relationship in NestJS

I am really struggling with this and I'm not sure why.

I have two tables: users and organizations. A user can belong to an organization, and an organization can have many users.

From this thread I thought I had resolved my issue, but it seems I haven't.

Here is my ormconfig.json file:

{
  "type": "postgres",
  "host": "localhost",
  "port": 5432,
  "username": "username",
  "password": "password",
  "database": "mydb",
  "logging": true,
  "entities": [
    "dist/**/*.entity{.ts,.js}"
  ],
  "synchronize": true
}

In my organization.entity.ts file I have this:

...
@PrimaryGeneratedColumn('uuid')
  id: string;

@Column()
  name: string;

@OneToMany(() => User, (user) => user.organization)
  users: User[];

In my user.entity.ts file, I have this:

@PrimaryColumn({
    type: 'uuid',
  })
  id: string;

@Column({ type: 'uuid' })
  organizationId: string;

@ManyToOne(() => Organization, (organization) => organization.users)
  @JoinColumn({
    name: 'organizationId',
  })
  organization: Organization;

When I start my local development server via npm run start:dev I get this error:

query failed: ALTER TABLE "users" ADD CONSTRAINT "FK_f3d6aea8fcca58182b2e80ce979" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE NO ACTION ON UPDATE NO ACTION

detail: 'Key columns "organizationId" and "id" are of incompatible types: character varying and uuid.', 

It seems that I'm setting both column types to be uuid, so I'm not sure where it's getting the different column types.

I have the same relationship without issue on my Organizations --> Locations. What am i doing wrong with the Organization --> User relationship to cause the error?



Solution 1:[1]

It turns out I must have had something very wrong with my application. I started a fresh NestJS application and everything works fine. Thank you for your suggestion!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Damon