Permalink
Browse files

fix(SnapshotManager): use nullable value for joinColumn from mapping

This is a expected behaviour as the documentation says so.

Solves #215
  • Loading branch information...
elxris committed Aug 20, 2017
1 parent d938f81 commit ce84a91bb1fb22340560eb110e02c4ca550256e3
@@ -516,7 +516,7 @@ export class SnapshotManager {
// We own the fk.
let joinColumn = mapping.fields[property].joinColumn;
let changes = {
field : {name: joinColumn.name, type: 'integer', unsigned: true, nullable: true},
field : {name: joinColumn.name, type: 'integer', unsigned: true, nullable: (typeof joinColumn.nullable === 'boolean' ? joinColumn.nullable : true)},
foreign: createForeign(mapping, property, targetMapping)
};
@@ -0,0 +1,22 @@
import {ArrayCollection} from '../../../../src/ArrayCollection';
import {Mapping} from '../../../../src/Mapping';
import {Publisher} from './publisher';
export class Book {
public id: Number;
public publisher: ArrayCollection<Publisher>;
public name: string;
static setMapping(mapping: Mapping<Book>) {
mapping.field('id', {type: 'integer'}).primary('id').generatedValue('id', 'autoIncrement');
mapping.field('name', {type: 'string', size: 24});
mapping
.manyToOne('publisher', {targetEntity: 'Publisher', inversedBy: 'books'})
.joinColumn('publisher', {nullable: false});
}
}
@@ -0,0 +1,22 @@
import {ArrayCollection} from '../../../../src/ArrayCollection';
import {Mapping} from '../../../../src/Mapping';
import {Book} from './book';
export class Publisher {
public id: number;
public name: string;
public books: ArrayCollection<Book>;
static setMapping(mapping: Mapping<Publisher>) {
mapping.forProperty('id')
.field({type: 'integer'})
.generatedValue('autoIncrement')
.primary();
mapping.field('name', {type: 'string', size: 24});
mapping.oneToMany('books', {targetEntity: 'Book', mappedBy: 'publisher'});
}
}
@@ -4,6 +4,8 @@ import {SchemaBuilder} from '../../src/SchemaBuilder';
import {SnapshotManager} from '../../src/SnapshotManager';
import oldEntities from '../resource/entity/snapshot/old';
import newEntities from '../resource/entity/snapshot/new';
import {Book} from '../resource/entity/book/book';
import {Publisher} from '../resource/entity/book/publisher';
function getWetland(entities?): Wetland {
let wetland = new Wetland({
@@ -48,4 +50,21 @@ describe('SnapshotManager', () => {
assert.equal(sqlStatement[1], 'alter table `media` add constraint `media_offer_id_foreign` foreign key (`offer_id`) references `offer` (`id`) on delete cascade');
});
});
describe('diff(jc): create join column', () => {
it('Should be able to create a non null foreign key', () => {
let oldMapping = getMapping([]),
newMapping = getMapping([Book, Publisher]);
let wetland = getWetland(),
snapshotManager = wetland.getSnapshotManager(),
schemaBuilder = new SchemaBuilder(wetland.getManager());
let diff = snapshotManager.diff(oldMapping, newMapping);
let sqlStatement = schemaBuilder.process(diff).getSQL().split('\n');
assert.equal(sqlStatement[0], 'create table `publisher` (`id` int unsigned not null auto_increment primary key, `name` varchar(24) not null);');
assert.equal(sqlStatement[1], 'create table `book` (`id` int unsigned not null auto_increment primary key, `name` varchar(24) not null, `publisher_id` int unsigned not null);');
})
});
});

0 comments on commit ce84a91

Please sign in to comment.