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

Lazy Relations Not Updating Properly #10822

Open
1 of 18 tasks
Ice-Hazymoon opened this issue Apr 9, 2024 · 2 comments
Open
1 of 18 tasks

Lazy Relations Not Updating Properly #10822

Ice-Hazymoon opened this issue Apr 9, 2024 · 2 comments

Comments

@Ice-Hazymoon
Copy link

Ice-Hazymoon commented Apr 9, 2024

Issue description

I encountered an issue while trying to update a lazy relation in TypeORM. When attempting to save a new Wallet entity and associate it with an existing User entity using a lazy relation, the wallet_id column in the user table is not being updated correctly.

Expected Behavior

user.wallet_id = wallet.id

Actual Behavior

No change in user data

Steps to reproduce

@Entity('user')
export class User {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({ type: 'int', nullable: true })
    wallet_id: number;

    @OneToOne(() => Wallet, (Wallet) => Wallet.user)
    @JoinColumn({ name: 'wallet_id' })
    wallet: Promise<Wallet>;
}

@Entity('wallet')
export class Wallet {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({ type: 'int' })
    user_id: number;

    @OneToOne(() => User, (user) => user.wallet, { eager: true })
    @JoinColumn({ name: 'user_id' })
    user: Relation<User>;

    @Column({ type: 'text' })
    name: string;
}

const user = await userRepository.findOne({
    where: { id: 1 },
});
const newWallet = new Wallet();
newWallet.user_id = user.id;
newWallet.name = 'foo';
const saveWallet = await WalletRepository.save(newWallet);
user.wallet = Promise.resolve(saveWallet);
await userRepository.save(user);
query: SELECT "User"."wallet_id" AS "User_wallet_id" FROM "user" "User" WHERE "User"."id" IN ($1) -- PARAMETERS: [727]
query: INSERT INTO "wallet"("user_id", "name") VALUES ($1, $2) RETURNING "id" -- PARAMETERS: [727, "foo"]

My Environment

Dependency Version
Operating System Ubuntu 20
Node.js version v20.11.1
Typescript version 5.4.4
TypeORM version 0.3.20

Additional Context

I referenced the steps in the documentation, but it didn't help much

https://typeorm.io/eager-and-lazy-relations#eager-and-lazy-relations

Relevant Database Driver(s)

  • aurora-mysql
  • aurora-postgres
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • spanner
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

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

Yes, I have the time, but I don't know how to start. I would need guidance.

@Ice-Hazymoon
Copy link
Author

I tried using user.wallet_id = newWallet.id; still nothing is updating. there is no change in the data in the user table.

@pro100coder
Copy link

Faced a similar problem with the ManyToMany relationship. If a new entity is created and a lazy relationship is added immediately, no record is added to the JoinTable. If I save the parent entity and then load it again, then the relationship is created.

I noticed that when a new entity is created, the field is called the same as the field in the entity, but if the entity is loaded from the database, the promice prefix is added to the field.

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

2 participants