最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin4浏览0评论

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.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论