mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
Fix many to many on delete option
This commit is contained in:
parent
eece0c33fb
commit
6323478eea
@ -66,7 +66,7 @@ For example to proper install oracle driver you need to follow all instructions
|
||||
To create an initial `ormconfig.json` file, run the following command:
|
||||
|
||||
```shell
|
||||
npm run setup:config
|
||||
cp ormconfig.travis.json ormconfig.json
|
||||
```
|
||||
|
||||
## Building
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
import {EntityMetadata} from "../metadata/EntityMetadata";
|
||||
import {ColumnMetadata} from "../metadata/ColumnMetadata";
|
||||
import {Connection} from "../connection/Connection";
|
||||
import {EntityMetadata} from "../metadata/EntityMetadata";
|
||||
import {ForeignKeyMetadata} from "../metadata/ForeignKeyMetadata";
|
||||
import {IndexMetadata} from "../metadata/IndexMetadata";
|
||||
import {RelationMetadata} from "../metadata/RelationMetadata";
|
||||
import {JoinTableMetadataArgs} from "../metadata-args/JoinTableMetadataArgs";
|
||||
import {Connection} from "../connection/Connection";
|
||||
import {RelationMetadata} from "../metadata/RelationMetadata";
|
||||
|
||||
/**
|
||||
* Creates EntityMetadata for junction tables.
|
||||
@ -133,14 +133,14 @@ export class JunctionEntityMetadataBuilder {
|
||||
referencedEntityMetadata: relation.entityMetadata,
|
||||
columns: junctionColumns,
|
||||
referencedColumns: referencedColumns,
|
||||
onDelete: "CASCADE"
|
||||
onDelete: relation.onDelete || "CASCADE"
|
||||
}),
|
||||
new ForeignKeyMetadata({
|
||||
entityMetadata: entityMetadata,
|
||||
referencedEntityMetadata: relation.inverseEntityMetadata,
|
||||
columns: inverseJunctionColumns,
|
||||
referencedColumns: inverseReferencedColumns,
|
||||
onDelete: "CASCADE"
|
||||
onDelete: relation.onDelete || "CASCADE"
|
||||
}),
|
||||
];
|
||||
|
||||
|
||||
@ -1,9 +1,11 @@
|
||||
import "reflect-metadata";
|
||||
import {expect} from "chai";
|
||||
|
||||
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils";
|
||||
|
||||
import {Connection} from "../../../../src/connection/Connection";
|
||||
import {Post} from "./entity/Post";
|
||||
import { PhoneBook } from "./entity/PhoneBook";
|
||||
import {Post} from "./entity/Post";
|
||||
import {expect} from "chai";
|
||||
|
||||
describe("columns > value-transformer functionality", () => {
|
||||
|
||||
@ -14,7 +16,7 @@ describe("columns > value-transformer functionality", () => {
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it.only("should marshal data using the provided value-transformer", () => Promise.all(connections.map(async connection => {
|
||||
it("should marshal data using the provided value-transformer", () => Promise.all(connections.map(async connection => {
|
||||
|
||||
const postRepository = connection.getRepository(Post);
|
||||
|
||||
|
||||
14
test/github-issues/2464/entity/Bar.ts
Normal file
14
test/github-issues/2464/entity/Bar.ts
Normal file
@ -0,0 +1,14 @@
|
||||
import { BaseEntity, Column, ManyToMany, PrimaryGeneratedColumn } from "../../../../src";
|
||||
|
||||
import { Entity } from "../../../../src/decorator/entity/Entity";
|
||||
import { Foo } from "./Foo";
|
||||
|
||||
@Entity()
|
||||
export class Bar extends BaseEntity {
|
||||
@PrimaryGeneratedColumn() id: number;
|
||||
|
||||
@Column() description: string;
|
||||
|
||||
@ManyToMany(type => Foo, foo => foo.bars)
|
||||
foos?: Foo[];
|
||||
}
|
||||
23
test/github-issues/2464/entity/Foo.ts
Normal file
23
test/github-issues/2464/entity/Foo.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { BaseEntity, JoinTable, ManyToMany, PrimaryColumn } from "../../../../src";
|
||||
|
||||
import { Bar } from "./Bar";
|
||||
import { Entity } from "../../../../src/decorator/entity/Entity";
|
||||
|
||||
@Entity("foo")
|
||||
export class Foo extends BaseEntity {
|
||||
@PrimaryColumn() id: number;
|
||||
|
||||
@JoinTable()
|
||||
@ManyToMany(() => Bar, bar => bar.foos, {
|
||||
cascade: ['insert', 'update'],
|
||||
onDelete: 'NO ACTION'
|
||||
})
|
||||
bars?: Bar[];
|
||||
|
||||
|
||||
@JoinTable()
|
||||
@ManyToMany(() => Bar, bar => bar.foos, {
|
||||
cascade: ['insert', 'update'],
|
||||
})
|
||||
otherBars?: Bar[];
|
||||
}
|
||||
49
test/github-issues/2464/issue-2464.ts
Normal file
49
test/github-issues/2464/issue-2464.ts
Normal file
@ -0,0 +1,49 @@
|
||||
import "reflect-metadata";
|
||||
|
||||
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
|
||||
|
||||
import {Connection} from "../../../src/connection/Connection";
|
||||
import { Foo } from "./entity/Foo";
|
||||
import { QueryFailedError } from "../../../src";
|
||||
import {expect} from "chai";
|
||||
|
||||
describe("github issues > #2464 - ManyToMany onDelete option not working", () => {
|
||||
|
||||
let connections: Connection[];
|
||||
before(async () => connections = await createTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"]
|
||||
}));
|
||||
|
||||
beforeEach(() => reloadTestingDatabases(connections));
|
||||
after(() => closeTestingConnections(connections));
|
||||
|
||||
it("should not delete when onDelete is 'NO ACTION'", () => Promise.all(
|
||||
connections.map(async connection => {
|
||||
const repo = connection.getRepository(Foo);
|
||||
|
||||
await repo.save({ id: 1, bars: [{ description: "test1" }] });
|
||||
|
||||
try {
|
||||
await repo.delete(1);
|
||||
expect.fail();
|
||||
} catch(e) {
|
||||
e.should.be.instanceOf(QueryFailedError);
|
||||
}
|
||||
|
||||
})
|
||||
));
|
||||
|
||||
it("should delete when onDelete is not set", () => Promise.all(
|
||||
connections.map(async connection => {
|
||||
const repo = connection.getRepository(Foo);
|
||||
|
||||
await repo.save({ id: 1, otherBars: [{ description: "test1" }] });
|
||||
await repo.delete(1);
|
||||
|
||||
const foo = await repo.findOne(1)
|
||||
expect(foo).to.be.undefined
|
||||
|
||||
})
|
||||
));
|
||||
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user