Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Postgres TypeError: Converting circular structure to JSON #6999

Open
1 of 20 tasks
theoparis opened this issue Nov 1, 2020 · 7 comments
Open
1 of 20 tasks

Postgres TypeError: Converting circular structure to JSON #6999

theoparis opened this issue Nov 1, 2020 · 7 comments

Comments

@theoparis
Copy link

Issue Description

So I am using postgresql with typeorm and nestjs and I have a user entity. When I run .findOne({username }) it works fine, but doing .find({}) to get all simply gives this error:

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property 'parser' -> object with constructor 'HTTPParser'       
    --- property 'socket' closes the circle
    at JSON.stringify (<anonymous>)
    at SelectQueryBuilder.<anonymous> (C:\Users\thebo\Documents\ProxyByrd\packages\backend\node_modules\.pnpm\typeorm@0.2.28\node_modules\typeorm\query-builder\SelectQueryBuilder.js:1553:67)

Expected Behavior

The following code should return the list of users to the backend:

    async findAll(): Promise<User[]> {
        return await this.usersRepository.find({ take: 5 });
    }

Actual Behavior

It errors with circular socket structure error:

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property 'parser' -> object with constructor 'HTTPParser'       
    --- property 'socket' closes the circle
    at JSON.stringify (<anonymous>)
    at SelectQueryBuilder.<anonymous> (C:\Users\thebo\Documents\ProxyByrd\packages\backend\node_modules\.pnpm\typeorm@0.2.28\node_modules\typeorm\query-builder\SelectQueryBuilder.js:1553:67)

Steps to Reproduce

  1. Use nestjs and typeorm
  2. Try to call .find({}) on an injected repository in a service.

My Environment

Dependency Version
Operating System Windows 10
Node.js version 14.0.0
Typescript version 4.0.5
TypeORM version 0.2.28

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • [x ] postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

Are you willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time, and I know how to start.
  • Yes, I have the time, but I don't know how to start. I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@imnotjames
Copy link
Contributor

Can you create a repository that exhibits this? I can't replicate it with TypeORM natively.

@theoparis
Copy link
Author

theoparis commented Nov 2, 2020

Sure, I have pushed the monorepo that I have to github
(development branch) You should be able to use the packages/backend folder directly without using the monorepo with pnpm.

Maybe it is a nestjs/typeorm issue and not just typeorm but I'm unable to to figure out the code that would be causing a problem other than typeorm itself.

Also, I've never really had this issue before so I am not sure how else I can reproduce it.

@ghost
Copy link

ghost commented Nov 17, 2020

We have the same problem. It occurs when we call manager.save(partyEntity).

This is how our entities look like:

@Unique("UK_uid", ["uid"])
@Entity("party")
export class PartyEntity {

    constructor(id: string, type: PartyType, uid: string) {
        this.id = id
        this.type = type
        this.uid = uid
    }

    @PrimaryGeneratedColumn('uuid')
    id: string

    @Column({
        type: "enum",
        enum: PartyType
    })
    type: PartyType

    @Column()
    uid: string

    @OneToMany(() => PartyContactEntity, partyContact => partyContact.party, {cascade: true})
    contacts: PartyContactEntity[]
}
@Entity("customer_contact")
export class PartyContactEntity {

    constructor(party: PartyEntity, contactMedium: ContactMediumEntity) {
        this.party = party;
        this.contactMedium = contactMedium;
    }

    @PrimaryColumn('uuid', {name: "party_id"})
    @ManyToOne(() => PartyEntity, party => party.contacts)
    party: PartyEntity

    @PrimaryColumn('uuid', {name: "contact_id"})
    @OneToOne(() => ContactMediumEntity)
    @JoinColumn({name: "contact_id", referencedColumnName: "id"})
    contactMedium: ContactMediumEntity

}

Is there an error in how we've built our entities?

@ghost
Copy link

ghost commented Nov 17, 2020

It happens in TypeORM 0.2.22 and 0.2.18 as well.
I guess, there is some modelling error on my side?

@Cleatuer
Copy link

@ErolPeel When saving a One-to-Many relation in TypeORM you need to be careful to only reference one side of the relation. For example when you want to save a PartyEntity with a PartyContactEntity, do it like this:

const partyContactEntity: PartyContactEntity = new PartyContactEntity()
partyContactEntity.contactMedium = savedPostalContactEntity.contactMedium
// important: do not set the partyContactEntity.party here as this will result in the 
// "TypeError: Converting circular structure to JSON" error when saving the contact with the party

const partyEntity: PartyEntity = new PartyEntity( ... )
partyEntity.contacts = [partyContactEntity]

manager.save(partyEntity)

@Miodav
Copy link

Miodav commented Aug 25, 2022

Hi got the same error message but on a different context (using createQueryBuilder) I resolved this by adding either :
.getOne()
.getRawOne()
.getMany()
.getRawMany()
Hope that can be useful

@gaffarmalik
Copy link

gaffarmalik commented May 5, 2024

You most likely have a reverse relationship between 2 of your tables involved in the query.
You have to avoid referencing the parent table in the child table. E.g You need to prevent something like this: Parent-> Child -> Parent.
I had to delete a parent reference within a child table.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants