I'm working on a NestJS project where I need to interact with a SQL Server database. One of the tables in the database has a name with dots, specifically dbo.Bonos.Bonos. I'm using TypeORM to manage the database interactions, and I'm encountering issues when trying to query this table.
When I attempt to perform any operations on the table dbo.Bonos.Bonos using TypeORM, I get the following error:
Error: Invalid object name 'dbo.Bonos.Bonos '."
It seems that TypeORM has trouble handling table names with dots. Here is how I have defined my entity:
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity({ schema: 'dbo', name: 'Bonos.Bonos' }) // **** IMPORTANT: Define the table and schema name ****
export class BonosEntity {
@PrimaryGeneratedColumn()
idBono: number;
@Column()
codigo: string;
@Column()
promocionId: string;
@Column()
NIF: string;
@Column()
CUPS: string;
@Column()
habilitado: number;
@Column({ type: 'date' })
fechaInicio: Date;
@Column({ type: 'date' })
fechaFin: Date;
@Column({ type: 'date' })
fechaUso: Date;
@Column()
importe: number;
}
Thank you for your suggestions. Here are the steps I have taken based on your comment:
enabled query logging in my TypeORM configuration:
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { TypeOrmModule } from '@nestjs/typeorm';
import { DataSourceConfig } from './config/data.source';
import { BonosModule } from './bonos/bonos.module';
@Module({
imports: [
ConfigModule.forRoot({
envFilePath: `.${process.env.NODE_ENV}.env`,
isGlobal: true,
}),
TypeOrmModule.forRoot({
...DataSourceConfig,
logging: true, // Enable query logging
}),
BonosModule,
],
})
export class AppModule {}
I created a synonym in SQL Server that acts as an alias for the table dbo.Bonos.Bonos:
CREATE SYNONYM [dbo].[BonosBonos] FOR [dbo].[Bonos.Bonos];
I then updated my entity to use this synonym:
@Entity({ schema: 'dbo', name: 'BonosBonos' })
After making these changes, it worked!!
I want to thank both users who suggested these solutions.😁