Skip to content

Instantly share code, notes, and snippets.

@memee

memee/Error Secret

Created June 30, 2021 07:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save memee/040284bf8aa0931996fc5ad15e358f3d to your computer and use it in GitHub Desktop.
Save memee/040284bf8aa0931996fc5ad15e358f3d to your computer and use it in GitHub Desktop.
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[];
}
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)
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