0

I have the Users entity:
id|name |age|images
1 | John | 4 | {123.jpg,258.png}
I want to delete an image from images column. According to typeorm documentation i can use:

import {getConnection} from "typeorm";

await getConnection()
    .createQueryBuilder()
    .delete()
    .from(Users)
    .where("id = :id", { id: 1 })
    .execute();

If use this i will delete the whole entity, but i need just to delete 123.jpg from images column.

Question: How to delete 123.jpg image from images column?
Note: I use NestJs and ProgresQl


Users entity:

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

    @Column("text",{nullable: true, array: true})
    images: string[];
}

7
  • @AnushKamble, do you know a solution? Commented Jan 29, 2021 at 14:55
  • Are images just a colmun in User entity? please share The user entity Commented Jan 29, 2021 at 14:57
  • @Youba, i updated. Indeed the images is just a column. Is there a solution? Commented Jan 29, 2021 at 15:00
  • @AnushKamble, you can add your answer Commented Jan 29, 2021 at 15:05
  • 1
    you should just update the colmun, you don't need anything to do with delete in your case Commented Jan 29, 2021 at 15:15

1 Answer 1

0
await getConnection()
    .createQueryBuilder()
    .update()
    .set({ images: [...valuesWithoutThatImage] })
    .where("id = :id", { id: 1 })
    .execute();

You need to update that object and set images to new desired value, in this case it should have all previous images without 123.jpg

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

Comments

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.