javascript - TypeORM Throwing Duplication Error on Bulk saving instead of ignore or update existing value - Stack Overflow

As Documented on TypeOrm FrameWork Repository.save should saveinsert new values and ignoreupdate the

As Documented on TypeOrm FrameWork Repository.save should save/insert new values and ignore/update the existing once,

But now I'm facing a problem that it's thrown a duplication error on existing value and stoping the whole inserting! ( I have a unique column called key )

My entity:

import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn, PrimaryColumn } from 'typeorm';
import { Source } from '../sources/source.entity';


@Entity({ name: 'articles' })
export class Article {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    title: string;

    @Column({
        nullable: true
    })
    image: string | null;

    @Column({
        type: "text",
    })
    url: string;

    @Column({
        type: "varchar",
        unique: true
    })
    key: string;

    @Column({
        type: 'datetime',
        nullable: true
    })
    date: Date | null;

    @ManyToOne(type => Source, source => source.articles, {eager: true})
    @JoinColumn({name: 'source'})
    source: Source;

    @Column({
        type: `text`,
        nullable: true
    })
    description: string | null
}

My Service:

constructor(
    @InjectRepository(Article) private readonly articleRepository: Repository<Article>,
    private readonly articlesScraper: BlogScraperService
) {

}

async clonningFromScraper() {
    let articles = await this.articlesScraper.articles('1');

    articles = articles.map(article => ({ ...article, key: decodeURIComponent(article.url).substring(0, 255) }));

    return this.articleRepository
        .save(articles);
}

As Documented on TypeOrm FrameWork Repository.save should save/insert new values and ignore/update the existing once,

But now I'm facing a problem that it's thrown a duplication error on existing value and stoping the whole inserting! ( I have a unique column called key )

My entity:

import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn, PrimaryColumn } from 'typeorm';
import { Source } from '../sources/source.entity';


@Entity({ name: 'articles' })
export class Article {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    title: string;

    @Column({
        nullable: true
    })
    image: string | null;

    @Column({
        type: "text",
    })
    url: string;

    @Column({
        type: "varchar",
        unique: true
    })
    key: string;

    @Column({
        type: 'datetime',
        nullable: true
    })
    date: Date | null;

    @ManyToOne(type => Source, source => source.articles, {eager: true})
    @JoinColumn({name: 'source'})
    source: Source;

    @Column({
        type: `text`,
        nullable: true
    })
    description: string | null
}

My Service:

constructor(
    @InjectRepository(Article) private readonly articleRepository: Repository<Article>,
    private readonly articlesScraper: BlogScraperService
) {

}

async clonningFromScraper() {
    let articles = await this.articlesScraper.articles('1');

    articles = articles.map(article => ({ ...article, key: decodeURIComponent(article.url).substring(0, 255) }));

    return this.articleRepository
        .save(articles);
}
Share Improve this question edited Feb 5, 2020 at 12:11 Ranjan R.P. 1,1433 gold badges13 silver badges29 bronze badges asked Feb 5, 2020 at 12:07 hesham shawkyhesham shawky 1,1516 gold badges22 silver badges48 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 4

I have ended up solving this by RAW SQL query using the following

return this.articleRepository.query(
    "INSERT IGNORE INTO articles ( title, date, url, image, source, description, _key ) VALUES ?", [querableArticles]);

If you are going to update the same record, which includes a unique constraint ( in your case, key ), first, you need to find the record from the DB and attach the ID to the new record that you want to update via the save method.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744583542a4582179.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信