0

I want to update a column which has a relation:

async update(req, res): Promise<any> {
        const {email, password, id} = req.body;
        try {
            await getConnection()
                .createQueryBuilder()
                .update(User)
                .set({
                    email: email,
                    password: password,
                    meta: [{   
                        name: "tesst",
                        message: "jasshd"
                    }]
                })
                .where("id = :id", {id: id})
                .execute();

            res.status(201).send({
                message: 'user updated'
            })

        } catch (e) {
            console.log('update', e)
            res.send({
                message: 'update error'
            })
        }
    }

Meta table is in relation with User table. Trying to update the user with the attached meta i get: EntityColumnNotFound: No entity column "meta" was found.. Why i get this error? Note: If i use this relation when i register the user like:

const user = this.usersRepository.create({
  email,
  password,
  meta: [{
    name: "tesst",
    message: "jasshd"
  }]
});
return await this.usersRepository.save(user);

... the code work. Why the code works when i register the user but not when i update it?

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

    @Column({nullable: true})
    email: string;

    @Column({nullable: true})
    password: string;


    @OneToMany(() => Meta, meta => meta.user, { cascade: true })
    meta: Meta[];
}


export class Meta {
    @PrimaryGeneratedColumn({name: metaId})
    metaId: number;

    @Column({nullable: true})
    name: string;

    @Column({nullable: true})
    message: string;

    @ManyToOne(() => User, user => user.meta)
    user: User;
}

5
  • 1
    Can you provide the meta & user entities to see which relation they have? Commented Jan 26, 2021 at 20:08
  • @Youba one to many Commented Jan 26, 2021 at 20:10
  • @Youba i updated the question with both entities. Could you help? Commented Jan 26, 2021 at 20:37
  • I don't think you can update the relation data from the user QueryBuilder, the solution is to create another meta QueryBuilder to update their data, I can provide an example of it you're positive with that solution Commented Jan 26, 2021 at 20:49
  • @Youba, it will be helpful. Could you show how to change my code? Commented Jan 26, 2021 at 20:51

1 Answer 1

1

I don't think you can update the relation data with querybuilder, the solution I see here is to update meta with itself :

async update(req, res): Promise<any> {
    const {email, password, id} = req.body;
    try {
        await getConnection()
            .createQueryBuilder()
            .update(User)
            .set({
                email: email,
                password: password,
                meta: [{   
                    name: "tesst",
                    message: "jasshd"
                }]
            })
            .where("id = :id", {id: id})
            .execute();
  // Update meta :
    await getConnection()
            .createQueryBuilder()
            .update(Meta)
            .set({ 
                    name: "tesst",
                    message: "jasshd"
            })
            .where("user = :id", {id: id})
            .execute();
        res.status(201).send({
            message: 'user updated'
        })

    } catch (e) {
        console.log('update', e)
        res.send({
            message: 'update error'
        })
    }
}

Ps: this operation will update all the data of meta that has user = :id

Sign up to request clarification or add additional context in comments.

7 Comments

is that what you want?
i changed a lit a bit the entity Meta above. And where i update the Meta i added: .where("userId = :userId", {userId: id}), but it does not add any changes in meta table. Why?
You should use user not userId
one question, possible not related to this one. I have User entity and UserDataEntity. User has as relation UserDataEntity. In this way using oneToMany, in UserDataEntity si created a column like userId. Is there a posibilty to access a row from UserDataEntity according to userId? Because even if the userId column is created but i get an error that userId column is not found. Could you help?
to access to user from UserDataEntity you should define the relation decorator for the userId
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.