I have 3 tables : CardTypeOne , Card , TransactionMoney .
CardTypeOne has a foreign key to card table (OneToOne relation). the card table has 2 foreign key to TransactionMoney Table (OneToMany relations) like below:
CardTypeOne Table:
@OneToOne(type => Card)
@JoinColumn()
card: Card;
Card Table is:
@OneToMany(
() => TransactionMoney,
transactionMoney => transactionMoney.fromCard,
)
outterTransaction: TransactionMoney[];
@OneToMany(
() => TransactionMoney,
transactionMoney => transactionMoney.toCard,
)
innerTransaction: TransactionMoney[];
TransactionMoney Table:
@Column('integer')
amount: number;
@ManyToOne(
() => Card,
card => card.withdraws,
)
outterTransaction: Card;
@ManyToOne(
() => Card,
card => card.deposit,
)
innerTransaction: Card;
TransactionMoney table has an amount field. I want to calculate the sum function on the outterTransaction on the amount field, and do same for innertransaction. Finally I'd like to subtract these summation. here is my try code:
await getConnection()
.getRepository(CardTypeOne)
.createQueryBuilder('typeOne')
.leftJoinAndSelect('typeOne.card', 'card')
.leftJoin('card.outterTransaction','outter')
.leftJoin('card.innerTransaction','inner')
.addSelect('sum (outter.amount) as balance1')
.addSelect('sum (inner.amount) as balance2')
.where('typeOne.id= :id', { id: id})
.getOne();
But when I run the code I got this error:
column "typeOne.id" must appear in the GROUP BY clause or be used in an aggregate function