-
-
Save memee/040284bf8aa0931996fc5ad15e358f3d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Exclude, Expose } from "class-transformer"; | |
import { | |
Column, | |
Entity, | |
ManyToMany, | |
PrimaryGeneratedColumn, | |
} from "typeorm"; | |
@Entity() | |
export class Project { | |
@Expose() | |
@PrimaryGeneratedColumn() | |
id: number; | |
@Expose() | |
@Column({ length: 50 }) | |
name: string; | |
// Relations | |
@Expose() | |
@ManyToMany(() => User, (user) => user.projects, { cascade: true }) | |
@JoinTable() | |
users: User[]; | |
} | |
@Entity() | |
export class User { | |
@Expose() | |
@PrimaryGeneratedColumn() | |
id: number; | |
@Expose() | |
@Column({ type: "varchar", length: 255, nullable: false, unique: true }) | |
email: string; | |
// Password and stuff | |
@Exclude() | |
@Column({ type: "varchar", length: 100, nullable: false }) | |
password: string; | |
@Expose() | |
@Column({ nullable: true, length: 50 }) | |
firstName?: string; | |
@Expose() | |
@Column({ nullable: true, length: 50 }) | |
lastName?: string; | |
@Expose() | |
@ManyToMany(() => Project, (project) => project.users) | |
projects: Project[]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
QueryFailedError: Cannot delete or update a parent row: a foreign key constraint fails (`db`.`project_users_user`, CONSTRAINT `FK_f8300efd87679e1e21532be9808` FOREIGN KEY (`userId`) REFERENCES `user` (`id`)) | |
at new QueryFailedError (../../../src/error/QueryFailedError.ts:11:9) | |
at Query.onResult (../../../src/driver/mysql/MysqlQueryRunner.ts:193:37) | |
at Query.execute (../../../node_modules/mysql2/lib/commands/command.js:30:14) | |
at PoolConnection.handlePacket (../../../node_modules/mysql2/lib/connection.js:425:32) | |
at PacketParser.onPacket (../../../node_modules/mysql2/lib/connection.js:75:12) | |
at PacketParser.executeStart (../../../node_modules/mysql2/lib/packet_parser.js:75:16) | |
at Socket.<anonymous> (../../../node_modules/mysql2/lib/connection.js:82:25) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe("test", () => { | |
const userIds = [1, 2, 3]; // some user ids | |
afterEach(async () => { | |
// here's the problem | |
await connection | |
.createQueryBuilder() | |
.delete() | |
.from(User) | |
.whereInIds(userIds) | |
.execute(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment