Skip to content

Instantly share code, notes, and snippets.

@maphe

maphe/Brand.ts Secret

Created November 3, 2020 22:09
Show Gist options
  • Save maphe/33e6bc2a59aedbc4afa79b8cf75498ae to your computer and use it in GitHub Desktop.
Save maphe/33e6bc2a59aedbc4afa79b8cf75498ae to your computer and use it in GitHub Desktop.
Mikro ORM v4 omitted column with sqlite
import { Collection, Entity, OneToMany, PrimaryKey } from '@mikro-orm/core';
import { BrandSiteRestriction } from './BrandSiteRestriction';
@Entity({ tableName: 'brands' })
export class Brand {
@PrimaryKey()
id!: number;
@OneToMany({ entity: () => BrandSiteRestriction, mappedBy: 'brand' })
brandSiteRestrictions = new Collection<BrandSiteRestriction>(this);
}
import { Entity, ManyToOne, PrimaryKey } from '@mikro-orm/core';
import { Brand } from './Brand';
import { Site } from './Site';
@Entity({ tableName: 'brand_site_restrictions' })
export class BrandSiteRestriction {
@PrimaryKey()
id!: number;
@ManyToOne({ entity: () => Site })
site!: Site;
@ManyToOne({ entity: () => Brand })
brand!: Brand;
}
import { Entity, ManyToOne, PrimaryKey } from '@mikro-orm/core';
import { Exclude, Expose } from 'class-transformer';
import { Publisher } from './Publisher';
import { Site } from './Site';
@Entity({ tableName: 'placements' })
@Exclude()
export class Placement {
@PrimaryKey()
@Expose({ groups: ['summary'] })
id!: number;
@ManyToOne({ entity: () => Publisher })
publisher!: Publisher;
@ManyToOne({ entity: () => Site })
site!: Site;
}
import { Collection, Entity, OneToMany, PrimaryKey } from '@mikro-orm/core';
import { Site } from './Site';
import { Exclude } from 'class-transformer';
@Entity({ tableName: 'publishers' })
@Exclude()
export class Publisher {
@OneToMany({ entity: () => Site, mappedBy: 'publisher' })
sites = new Collection<Site>(this);
@PrimaryKey()
id!: number;
}
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { BrandSiteRestriction } from './BrandSiteRestriction';
import { Placement } from './Placement';
import { Publisher } from './Publisher';
@Entity({ tableName: 'sites' })
export class Site {
@ManyToOne({ entity: () => Publisher })
publisher: Publisher;
@OneToMany({ entity: () => Placement, mappedBy: 'site' })
placements = new Collection<Placement>(this);
@OneToMany({ entity: () => BrandSiteRestriction, mappedBy: 'site' })
brandSiteRestrictions = new Collection<BrandSiteRestriction, Site>(this);
@PrimaryKey()
id!: number;
@Property({ length: 191, nullable: true })
name: string;
}
import { BrandSiteRestriction } from './BrandSiteRestriction';
import { Site } from './Site';
import { MikroORM } from '@mikro-orm/core';
import { AbstractSqlDriver } from '@mikro-orm/sqlite';
import { Brand } from './Brand';
import { Publisher } from './Publisher';
import { Placement } from './Placement';
describe('BrandSiteRestriction', () => {
let orm;
describe('populateForeignKeys', () => {
beforeEach(async () => {
orm = await MikroORM.init<AbstractSqlDriver>({
entities: [BrandSiteRestriction, Site, Brand, Publisher, Placement],
name: 'db_test',
dbName: `db_test`,
type: 'sqlite',
discovery: { disableDynamicFileAccess: true },
cache: { enabled: false },
debug: ['query', 'query-params'],
});
const generator = orm.getSchemaGenerator();
await generator.dropSchema();
await generator.createSchema();
});
it('sets keys from references', async () => {
const site = new Site();
const br = new BrandSiteRestriction();
br.site = site;
await orm.em.persistAndFlush(br);
// OUTPUT: DriverException: insert into `brand_site_restrictions` (`site_id`) values (1) - SQLITE_ERROR: table brand_site_restrictions has no column named site_id
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment