Comments (5)
It is. Have a UNIQUE key in the database, and do :on_duplicate_key_update => '1=1'
from activerecord-import.
@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.
@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.
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.
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)
- Import with synchronize docs issue
- How can I use import to do upsert in a model that has 2 unique indexes? HOT 1
- Proposal: `.import` should accept array-like objects
- Upcoming breakage with next Rails release HOT 8
- RuntimeError: Hstore value should be a hash HOT 3
- Inconsistent enum behavior
- Inserted Primary IDs not sequential HOT 2
- why import do not return imported ids, when it called in sidekiq worker ? HOT 6
- Allow import of ignored columns - Suggested Feature HOT 1
- Empty ON CONFLICT targets HOT 1
- Question about performance: What are the order of things? HOT 5
- insert with existing association does not seem to work
- NonNull Violation and calling nextval() on sequence_name when primary key is uuid
- Failing imports are incrementing serial_sequence HOT 1
- `@schema_columns_hash` is not cleared by `.reset_column_information`. HOT 8
- alias_attribute causing "specified more than once" error upon import. HOT 6
- Support `trilogy` adapter HOT 7
- Using :on_duplicate_key_ignore option with conflict targets
- import! method is not raising when unique constraint violated while still inserting some records HOT 6
- Model#valid? Returning True For Invalid Records After Import HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from activerecord-import.