views:

45

answers:

2

How do I use migrations to create a postgres sequence?

I have created a migrations file with the following executes:

execute 'CREATE SEQUENCE "ServiceAvailability_ID_seq";'
execute <<-SQL
  ALTER TABLE "ServiceAvailability" 
    ALTER COLUMN "ID" set DEFAULT NEXTVAL("ServiceAvailability_ID_seq")
SQL

However, when running rake db:migrate I'm getting the following error:

PGError: ERROR: column "ServiceAvailability_ID_seq" does not exist : ALTER TABLE "ServiceAvailability" ALTER COLUMN "ID" set DEFAULT NEXTVAL("ServiceAvailability_ID_seq")

Second question, rake db:schema:dump is meant to create a file that can be portably used against any DB, however, on investigating a schema:dump it appears to only include table, field and field type information and misses out indexes and sequences?

A: 

This worked

    execute 'CREATE SEQUENCE ServiceAvailability_ID_seq;'
execute <<-SQL
  ALTER TABLE "ServiceAvailability" 
    ALTER COLUMN "ID" set DEFAULT NEXTVAL('ServiceAvailability_ID_seq');
SQL
RewbieNewbie
A: 

Actually you should not use migrations like that, in future if you change your db to MySQL or any other db(rails supported) you will face problems. Please check the Ruby on Rails guides on the link given below.

http://guides.rubyonrails.org/migrations.html

Vamsi