Code Monkey home page Code Monkey logo

Comments (5)

saizai avatar saizai commented on June 28, 2024

It is. Have a UNIQUE key in the database, and do :on_duplicate_key_update => '1=1'

from activerecord-import.

zdennis avatar zdennis commented on June 28, 2024

@wycats, this works exactly as @saizai mentioned for MySQL using ON DUPLICATE KEY UPDATE support that's baked into MySQL.

PostgreSQL doesn't support this natively for bulk inserts that I'm aware of. All of the PostgreSQL solutions I've seen have involved database triggers or locking tables, copying to temporary table, massaging rows, dropping a table and then inserting from the temp one.

If you or anyone else knows of a more DB agnostic way to do this I'd love to make that happen.

from activerecord-import.

zdennis avatar zdennis commented on June 28, 2024

@wycats, I may have missed your request. You are only wanting to insert new rows (not insert and update at the same time, correct?

It's the auto-update part that I can't do in a DB-agnostic way, but simply inserting new rows may be feasible with what you suggest.

from activerecord-import.

mahemoff avatar mahemoff commented on June 28, 2024

BTW :on_duplicate_key_update => '1=1'' doesn't work.

:on_duplicate_key_update => Book.column_names does work, although the resulting SQL is verbose as it will contain a=values(a), b=values(b) etc.

from activerecord-import.

jkowens avatar jkowens commented on June 28, 2024

Postgres 9.5 and up now supports upsert functionality and activerecord-import has added support for it. There is also the option to use :on_duplicate_key_ignore.

So currently Postgres, MySQL, and SQLite have options for ignoring duplicate keys. At some point it would be nice if they all shared the same option for adding that to the sql statement. That can have its own issue opened though 😜

from activerecord-import.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.