I
I
Ivan Nesterovich2014-07-24 10:19:56
PostgreSQL
Ivan Nesterovich, 2014-07-24 10:19:56

[rails] How to import seed data with id in postgresql?

rake db:seed RAILS_ENV=production
All data with id (this is how I use paperclip, and the id of the record is important for it, since the photo is saved along this path)
After executing the code, the data is written to the database, but after that I cannot create a record:

ОШИБКА:  повторяющееся значение ключа нарушает ограничение уникальности "pages_pkey"
DETAIL:  Ключ "(id)=(3)" уже существует.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey, 2014-07-24
@mastedm

You need to shift the sequence in the database
www.postgresql.org/docs/9.1/static/functions-seque...

E
Eugene Burmakin, 2014-07-24
@Freika

I can’t say for sure, but it seems that in a similar case I did something with postgres, forcing it to start glowing not from one (for IDs, of course), but from imported n + 1.

D
Dmitry, 2014-07-27
@deemytch

I have the following migrate from project to project:

namespace :db do
  desc "Установить все счётчики ID в актуальное значение"
  task :reset_seq => :environment do
    query = "SELECT  'SELECT SETVAL(' ||quote_literal(quote_ident(S.relname))|| ', MAX(' ||quote_ident(C.attname)|| ') ) FROM ' ||quote_ident(T.relname)|| ';'
FROM pg_class AS S, pg_depend AS D, pg_class AS T, pg_attribute AS C
WHERE S.relkind = 'S'
    AND S.oid = D.objid
    AND D.refobjid = T.oid
    AND D.refobjid = C.attrelid
    AND D.refobjsubid = C.attnum
ORDER BY S.relname;"
     ActiveRecord::Base.connection.execute(query).each{ |s| ActiveRecord::Base.connection.execute(s["?column?"])}
  end

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question