Description
Thank you very much in advance. When using @joinTable specifying the name of the table and columns, the save method for multiple instances does not work by saving only the first one
I have a many-to-many relationship (N-> N), when users has where several users can have several types,I have the following tables:
Table users:
| Columns | types |
|---|---|
| id | uuidv4 |
| name | string |
| string |
Table users_types:
| Columns | types |
|---|---|
| id | uuidv4 |
| name | string |
| description | string |
Table users_users_types:
| Columns | types |
|---|---|
| id | uuidv4 |
| user_id | uuid |
| user_type_id | uuid |
Models: User:
import {
Column,
CreateDateColumn,
DeleteDateColumn,
Entity,
JoinTable,
ManyToMany,
PrimaryColumn,
UpdateDateColumn,
} from "typeorm";
import { TypeUser } from "@modules/accounts/infra/typeorm/entities/TypeUser";
@Entity("users")
class User {
@PrimaryColumn()
id?: string;
@Column()
name: string;
@Column({ unique: true })
email: string;
@ManyToMany((type) => TypeUser, (type_user) => type_user.users, {
cascade: true,
})
@JoinTable({
name: "users_types_users",
joinColumns: [{ name: "user_id", referencedColumnName: "id" }],
inverseJoinColumns: [{ name: "user_type_id", referencedColumnName: "id" }],
})
types?: TypeUser[];
@CreateDateColumn()
created_at?: Date;
@UpdateDateColumn()
updated_at?: Date;
@DeleteDateColumn()
deleted_at?: Date;
}
export { User };
TypeUser:
import {
Column,
CreateDateColumn,
DeleteDateColumn,
Entity,
JoinTable,
ManyToMany,
PrimaryColumn,
UpdateDateColumn,
} from "typeorm";
import { User } from "./User";
@Entity("types_users")
class TypeUser {
@PrimaryColumn()
id?: string;
@Column()
name: string;
@Column()
active: boolean;
@ManyToMany((type) => User, (user) => user.types)
users?: User[];
@CreateDateColumn()
created_at?: Date;
@UpdateDateColumn()
updated_at?: Date;
@DeleteDateColumn()
deleted_at?: Date;
}
export { TypeUser };
I'm assembling the seeds to test the insertion with the following code:
import { getConnection, MigrationInterface, QueryRunner } from "typeorm";
import { TypeUser } from "@modules/accounts/infra/typeorm/entities/TypeUser";
import { User } from "@modules/accounts/infra/typeorm/entities/User";
import { UsersTypesFactory } from "@shared/infra/typeorm/factories";
export class CreateUsersTypes1620665114995 implements MigrationInterface {
public async up(): Promise<void> {
const users = (await getConnection("seed")
.getRepository("users")
.find()) as User[];
const usersTypesFactory = new UsersTypesFactory();
const types = usersTypesFactory.generate();
await getConnection("seed").getRepository("types_users").save(types);
const types_list = (await getConnection("seed")
.getRepository("types_users")
.find()) as TypeUser[];
const types_users_list = Array.from({
length: types_list.length,
}).map((_, index) => types_list[index]) as TypeUser[];
users[0].types = types_users_list;
const relationshipUsersTypes = users[0];
await getConnection("seed")
.getRepository(User)
.save(relationshipUsersTypes);
}
public async down(): Promise<void> {
await getConnection("seed").getRepository("users_types_users").delete({});
await getConnection("seed").getRepository("types_users").delete({});
}
}
when executing the code the logs seem to be right:
query: START TRANSACTION
query: INSERT INTO "users_types_users"("user_id", "user_type_id") VALUES ($1, $2), ($3, $4), ($5, $6) -- PARAMETERS: ["8b90cacb-52bc-4635-bd2c-bc87d59b0d4d","9144899a-6380-4be6-8251-947b5bdccda9","8b90cacb-52bc-4635-bd2c-bc87d59b0d4d","7711c9a8-10c4-407d-a868-82234e85c614","8b90cacb-52bc-4635-bd2c-bc87d59b0d4d","581417e3-0790-4ea7-9bff-ca0f51bb16a4"]
query: COMMIT
query: INSERT INTO "migrations"("timestamp", "name") VALUES ($1, $2) -- PARAMETERS: [1620665114995,"CreateUsersTypes1620665114995"]
logger this instance save
User {
id: '8b90cacb-52bc-4635-bd2c-bc87d59b0d4d',
name: 'Brice',
last_name: 'Will',
cpf: 'xfpfnxowprx',
rg: '91r7aujxsl',
email: 'Toney.Bailey@hotmail.com',
password_hash: 'fe6eYzQXKp3kHyt',
birth_date: 2020-07-17T04:46:14.816Z,
created_at: 2021-05-12T05:43:22.064Z,
updated_at: null,
deleted_at: null,
types: [
TypeUser {
id: '9144899a-6380-4be6-8251-947b5bdccda9',
name: 'provider',
active: true,
created_at: 2021-05-12T05:43:22.291Z,
updated_at: null,
deleted_at: null
},
TypeUser {
id: '7711c9a8-10c4-407d-a868-82234e85c614',
name: 'client',
active: true,
created_at: 2021-05-12T05:43:22.291Z,
updated_at: null,
deleted_at: null
},
TypeUser {
id: '581417e3-0790-4ea7-9bff-ca0f51bb16a4',
name: 'admin',
active: true,
created_at: 2021-05-12T05:43:22.291Z,
updated_at: null,
deleted_at: null
}
]
}
but in the database only the content of the first one was saved content users_types_users
content type_users
content users

Expected Behavior
the expected result is that all available types are registered
Actual Behavior
only the first element of the type class instance is registered
Steps to Reproduce
- create a database with tables
- includes data in table
My Environment
| Dependency | Version |
|---|---|
| Operating System | |
| Node.js version | v14.16.1 |
| Typescript version | v4.2.3 |
| TypeORM version | v0.2.32 |
- [x]
postgres
I added this as a Bug but, I wanted to answer the question if someone has already gone through the same problem or managed to solve it?