These is my table:
#Entity('localidades')
export class Localidades {
#PrimaryGeneratedColumn()
id: number;
#Column({type:"varchar",length:100})
nombre: string;
#ManyToOne(type => Provincias)
#JoinColumn({name: "idprovincia"})
provincia: string;
}
This is my query:
async find(id: number): Promise<Localidades> {
const tabla = await this.repository.findOne(id)
return tabla
}
Obtained result:
{
"id": 1,
"nombre": "Localidad 1",
}
Expected result:
{
"id": 1,
"nombre": "Localidad 1",
"idprovincia": 1
}
I need the province id field to show it too
Try to add relations to your query:
async find(id: number): Promise<Localidades> {
const tabla = await this.repository.findOne(id, { relations: ['provincia'] })
return tabla
}
I fix it by defining my table as follows:
#PrimaryGeneratedColumn()
id: number;
#Column({type:"varchar",length:100})
nombre: string;
#Column()
idprovincia: number;
#ManyToOne(type => Provincias, {
cascade: true,
nullable: false,
})
#JoinColumn({name: "idprovincia"})
provincia: string;
And in the consultation
async find(id: number): Promise<Localidades> {
const tabla = await this.repository.findOne(id)
return tabla
}
I hope that it would be useful to someone.
Related
I have two tables: user and pet
My goal is to GET all active users active pets (active means enabled is true).
When I try to join those two tables to be able to filter active users I am getting 500 error.
I have used getRawMany and getMany, does not help.
#Entity({ name: 'pet' })
export class Pet {
#PrimaryColumn()
id: number;
#Column()
user_id: number;
#Column()
name: string;
#Column()
enabled: boolean;
#ManyToOne(() => User, (user) => user.pets)
user: User;
}
#Entity({ name: 'user' })
export class User {
#PrimaryColumn()
user_id: number;
#Column()
name: string;
#Column()
enabled: boolean;
#OneToMany(() => Pet, (pet) => pet.user)
pets: Pet[];
}
// Service
async getActiveUsersActivePets(
pageOptionsDto: PageOptionsDto,
relations: string[] = [],
throwsException = false,
): Promise<PageDto<Pet>> {
const queryBuilder = this._petsRepository
.createQueryBuilder('pet');
queryBuilder
.leftJoinAndSelect('pet.user', 'user')
.where('enabled = :enabled', { enabled: true })
.skip(pageOptionsDto.skip)
.take(pageOptionsDto.take);
const itemCount = await queryBuilder.getCount();
const entities = await queryBuilder.getRawMany();
const pageMetaDto = new PageMetaDto({ itemCount, pageOptionsDto });
return new PageDto(entities, pageMetaDto);
}
// Error
{
"statusCode": 500,
"error": "Internal Server Error",
"message": "",
"path": "/api/v2/pets",
"method": "GET",
"timeStamp": "2022-04-28T13:53:18.863Z"
}
I want to add query parameter to my queryBuilder but it returns error invalid input syntax for type json. What is the correct way to send this parameter?
My query:
const foundedUserContacts = await this.userBookContactRepository
.createQueryBuilder('userBookContacts')
.select(['userBookContacts.contacts'])
.andWhere(`contacts #> '[{ "phoneNumber": :phoneNumber }]'`, {
phoneNumber: user.phoneNumber,
})
.getMany();
This works ok:
const foundedUserContacts = await this.userBookContactRepository
.createQueryBuilder('userBookContacts')
.select(['userBookContacts.contacts'])
.andWhere(`contacts #> '[{ "phoneNumber": "${user.phoneNumber}" }]'`)
.getMany();
My schema is:
#Entity({ name: 'userBookContacts' })
#Unique('userUuid_userPhone', ['userUuid', 'phoneNumber'])
export class UserBookContact {
#PrimaryGeneratedColumn()
id: number;
#Column()
userUuid: string;
#Index()
#Column()
phoneNumber: string;
#Column({
type: 'jsonb',
nullable: true,
default: '[]',
})
contacts: UserContact[];
}
These are my tables:
#Entity('personas')
export class Personas {
#PrimaryGeneratedColumn()
#IsNumber()
id: number;
#Column({type:"varchar",length:100})
#IsNotEmpty()
nombre: string;
#OneToMany(type => Contactos, contactos => contactos.idpersona, {cascade: true})
contactos : Contactos[]
}
#Entity('contactos')
export class Contactos {
#PrimaryGeneratedColumn()
#IsNumber()
id: number;
#Column()
#IsNotEmpty()
idpersona: number;
#ManyToOne(type => Personas, {
})
#JoinColumn({name: "idpersona"})
persona: string;
#Column({type:"varchar",length:100})
nombre: string;
#Column({type:"varchar",length:11})
telefono: string;
}
This is the body of the query to add the records:
{
"nombre":"TestPersona",
"contactos":[{
"nombre":"TestContacto",
"telefono": "123456789"}
]
}
This is the error: [ExceptionsHandler] Field 'idpersona' doesn't have a default value.
It is assumed that the field idPersona in Contacts, should be inserted automatically. What am I doing wrong?. From already thank you very much.
I think you need to add cascade: ['insert'] and possibly add the inverseSide (second param) to #OneToMany:
#Entity({ name: 'persona' })
export class Persona {
#PrimaryGeneratedColumn()
public id: number
#Column()
public nombre: string
#OneToMany(() => Contacto, (contacto) => contacto.idpersona, {
cascade: ['insert']
})
public contactos: Contacto[]
}
#Entity({ name: 'contacto' })
export class Contacto {
#PrimaryGeneratedColumn({
name: 'id',
})
public id: number
#ManyToOne(() => Persona, (persona) => persona.contactos, {
nullable: false,
})
#JoinColumn({ name: 'idpersona' })
#Column()
public idpersona: string
#Column()
public telefono: string
}
Then you can do:
const persona = {
nombre: 'nombre',
contactos: [{
telefono: '123456789',
}]
}
const personaGuardada = await entityManager.getRepository('persona').save([persona])
I leave the way I fix it, in case it helps someone in the future.
In personas changed:
#OneToMany(() => Contacto, (contacto) => contacto.idpersona, {
cascade: ['insert']
})
public contactos: Contacto[]`
with:
#OneToMany(type => Contactos, contactos => contactos.personas,{
cascade : true,
})
public contactos: Contactos[];`
In contactos changed:
#Column()
#IsNotEmpty()
idpersona: number;
#ManyToOne(type => Personas, {
})
#JoinColumn({name: "idpersona"})
persona: string;`
with:
#ManyToOne(() => Personas, personas => personas.id, {nullable: false})
#JoinColumn({name: "idpersona"})
personas: Personas;
This is my table:
#Entity('products')
export class Products {
#PrimaryGeneratedColumn()
#IsNumber()
public id: number;
#Column('decimal', {
precision: 20,
scale: 2,
transformer : {
to (value) {
return value ;
},
from (value) {
return parseFloat (value) ;
},
},
})
#IsNotEmpty()
price: number;
}
In my ormconfig file I have:
...
bigNumberStrings: false,
supportBigNumbers: true,
If I do:
return await this.repository.find();
Everything works correctly. The price field returns it as a decimal.
But if I use createQueryBuilder it brings it back as a string:
return await this.repository.createQueryBuilder("products")
.select([
'products.id AS id',
'products.price AS price'])
.getRawMany();
Please, could someone tell me how I can fix it?
I have a Patient entity:
#Entity()
#ObjectType()
#InputType('PatientInput')
export class Patient extends BaseEntity {
#PrimaryGeneratedColumn()
#Field(type => Int, { nullable: true })
id: number
#Column({ length: 500 })
#Field({ nullable: true })
firstName?: string
#Column({ length: 500 })
#Field({ nullable: true })
lastName?: string
#Field(type => MedicalCondition, { nullable: true })
#OneToMany(type => MedicalCondition, medicalCondition => medicalCondition.patient, { cascade: true })
medicalConditions?: MedicalCondition[]
}
And a MedicalCondition entity:
#ObjectType()
#Entity()
#InputType('medicalCondition')
export class MedicalCondition extends BaseEntity {
#Field({nullable: true})
#PrimaryGeneratedColumn()
id: number
#Field(type => Int, { nullable: true })
#RelationId((medicalCondition: MedicalCondition) => medicalCondition.patient)
#Column()
patientId?: number
#Field(type => Patient, { nullable: true })
#ManyToOne(type => Patient, patient => patient.medicalConditions, {
onDelete: 'CASCADE',
})
#JoinColumn()
patient?: Patient
#Field()
#Column()
name: string
#Field({nullable: true})
#Column()
startDate?: Date
}
When trying to create an instance of patient using the BaseEntity create function, the patient is created but in the medical conditions array only containes the last element and all the rest disappear even if there were many elements.
#Mutation(returns => Patient)
async create(#Args('patient') newPatientInput: Patient, #CurrentUser() user: User, #Useragent() useragent): Promise<Patient> {
const newPatient: Patient = Patient.create(newPatientInput)
const event = createEvent({ useragent, actionType: 'add_patient', userId: user.id })
const p = await this.patientService.create(newPatient, event)
return p
}
create = async (patient: Patient, event: EventLog): Promise<Patient> => {
const isExist = await this.isPatientExist({ firstName: patient.firstName, lastName: patient.lastName, birthDate: patient.birthDate })
if (isExist > 0) {
throw new Error('Patient already exist')
} else {
const transactionResult = runTransaction(async (em) => {
const eventLog = EventLog.create(event)
await em.save(eventLog)
return await em.save(patient)
})
return transactionResult
}
}
I tried to directly save the entity without invoting create():
return await em.save(Patient, patient)
and that was the result of the saving:
medicalConditions:
[ { id: 36,
name: 'heartDisease',
startDate: 2016-07-31T21:00:00.000Z,
kin: 'father',
note: '',
patientId: 26,
createdAt: 2019-11-24T17:11:22.376Z,
updatedAt: 2019-11-24T17:11:22.376Z },
{ id: null,
name: 'previousHeartAttack',
startDate: 2018-04-30T21:00:00.000Z,
kin: 'brother',
note: '' },
{ id: null,
name: 'highBloodPressure',
startDate: 2018-03-31T21:00:00.000Z,
kin: 'sister',
note: '' } ],
Tried google for it and didn't find any known issue.
So the willing result would be to create an entity deeply, is that a possible behavior?