I am faced with the challenge of creating tables in two different databases using TypeORM. Each table is associated with a specific database through the use of the @Entity
decorator. However, I encounter an error stating that user x does not have write access to database y.
How can I correctly specify the databases for each entity?
// src/entity/User.ts
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
@Entity({database: 'db1'})
export class User {
@PrimaryGeneratedColumn()
id: number
@Column()
name: string
}
// src/entity/Movie.ts
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'
@Entity({database: 'db2'})
export class Movie {
@PrimaryGeneratedColumn()
id: number
@Column()
title: string
}
The connections are established with the correct credentials:
// src/index.ts
await createConnection({
name: 'connection1',
host: 'SERVER1',
username: 'bob',
password: 'xxx',
type: 'mssql',
database: 'db1',
synchronize: true,
entities: ['src/entity/**/*.ts'],
migrations: ['src/migration/**/*.ts'],
subscribers: ['src/subscriber/**/*.ts'],
cli: {
entitiesDir: 'src/entity',
migrationsDir: 'src/migration',
subscribersDir: 'src/subscriber',
},
})
await createConnection({
name: 'connection2',
host: 'SERVER2',
username: 'mike',
password: 'xxx',
type: 'mssql',
database: 'db2',
synchronize: true,
entities: ['src/entity/**/*.ts'],
migrations: ['src/migration/**/*.ts'],
subscribers: ['src/subscriber/**/*.ts'],
cli: {
entitiesDir: 'src/entity',
migrationsDir: 'src/migration',
subscribersDir: 'src/subscriber',
},
})
Decorators play a crucial role as we also utilize type-graphql
decorators within our class structure. Interestingly, when the entity decorator is left blank, both tables are created in both databases indicating the correct credentials are used.
A similar issue has been discussed here and assistance has been requested here.
Thank you for your assistance.