Skip to content

Instantly share code, notes, and snippets.

@gthole
Last active September 12, 2024 15:45
Show Gist options
  • Save gthole/b86f60fc5a885301f5ca883d9b8dddfe to your computer and use it in GitHub Desktop.
Save gthole/b86f60fc5a885301f5ca883d9b8dddfe to your computer and use it in GitHub Desktop.
TypeORM Issue Example
lib/
node_modules/
package-lock.json

TypeORM Issue Example

This project is a minimum viable example to illustrate an issue with TypeORM ManyToMany relationship inserts via Docker.

To run this example you will need bash and docker-compose installed.

To reproduce the issue, run the following:

# Clone this gist
$ git clone https://gist.github.com/b86f60fc5a885301f5ca883d9b8dddfe.git type-orm-issue-11060

$ Change into the directory
$ cd type-orm-issue-11060

# Install npm dependencies
$ docker-compose run --rm dev npm install

# Run the example
$ docker-compose run --rm dev npm test

This will run ./src/index.ts and result in an error illustrating the issue.

import {
Entity,
PrimaryGeneratedColumn,
JoinTable,
ManyToMany,
} from 'typeorm';
import { Group } from './group';
export enum SignalStyle {
reduceTo = 'reduceTo',
reduceBy = 'reduceBy',
}
@Entity('client')
export class Client {
@PrimaryGeneratedColumn()
id: number;
@ManyToMany(() => Group, (group) => group.clients, {cascade: true})
@JoinTable({
name: 'client_groups',
joinColumn: {
name: 'client_id',
referencedColumnName: 'id'
},
inverseJoinColumn: {
name: 'group_id',
referencedColumnName: 'id'
}
})
groups: Group[];
}
version: '3'
services:
db:
image: 'postgres:16-alpine'
environment:
POSTGRES_DB: 'example'
POSTGRES_PASSWORD: 'example'
POSTGRES_USER: 'example'
logging:
driver: 'none'
dev:
image: 'node:18-alpine'
working_dir: '/app'
volumes:
- './:/app'
depends_on:
- 'db'
links:
- 'db'
import {
Entity,
PrimaryGeneratedColumn,
ManyToMany,
JoinTable,
} from 'typeorm';
import { Client } from './client';
@Entity('group')
export class Group {
@PrimaryGeneratedColumn()
id: number;
@ManyToMany(() => Client, (client) => client.groups)
@JoinTable({
name: 'client_groups',
joinColumn: {
name: 'group_id',
referencedColumnName: 'id'
},
inverseJoinColumn: {
name: 'client_id',
referencedColumnName: 'id'
}
})
clients: Client[];
}
import 'reflect-metadata'
import { Repository, DataSource, DataSourceOptions } from 'typeorm';
import { Group } from './group';
import { Client } from './client';
async function run() {
const options: DataSourceOptions = {
type: 'postgres',
host: 'db',
port: 5432,
username: 'example',
password: 'example',
database: 'example',
synchronize: true,
entities: [__dirname + '/client.js', __dirname + '/group.js'],
};
const connectionDB = new DataSource(options);
const connection = await connectionDB.initialize();
const clients = connection.getRepository(Client);
const groups = connection.getRepository(Group);
const c = await clients.save({groups: []});
const g1 = await groups.save({});
const g2 = await groups.save({});
console.log(`client id = ${c.id}`);
console.log(`group id = ${g2.id}`);
const updated = clients.merge(c, {groups: [g2]});
await clients.save(updated);
}
if (require.main === module) {
run();
}
{
"name": "typeorm-issue-example",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"test": "rm -rf ./lib && tsc && node ./lib/index"
},
"author": "",
"license": "ISC",
"description": "",
"dependencies": {
"@types/node": "22.5.4",
"pg": "8.12.0",
"typeorm": "0.3.20",
"typescript": "5.6.2"
}
}
{
"compilerOptions": {
"module": "commonjs",
"target": "es2020",
"moduleResolution": "Node",
"strict": false,
"rootDir": "./",
"outDir": "./lib",
"removeComments": false,
"esModuleInterop": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2020","DOM"
],
"declaration": true,
"declarationMap": true,
"inlineSourceMap": false
},
"types": ["node"],
"include": [
"*.ts"
],
"exclude": [
"node_modules"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment