hungry fish
hungry fish

Reputation: 188

Getting following error on migration

I am working on rails 2 project and am getting the following error while running rake tasks. Can someone help me out as what may be causing this.

[root@localhost webapp]# rake db:migrate
(in /root/public/webapp)
==  CreateWhereKeywords: migrating ============================================
-- create_table(:where_keywords)
NOTICE:  CREATE TABLE will create implicit sequence "where_keywords_id_seq" for serial column "where_keywords.id"
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "where_keywords_pkey" for table "where_keywords"
   -> 0.0838s
-- execute("alter table where_keywords add constraint where_keyword foreign key (where_location_id) references \n        where_locations(id) on delete cascade")
rake aborted!
An error has occurred, this and all later migrations canceled:

PGError: ERROR:  foreign key constraint "where_keyword" cannot be implemented
DETAIL:  Key columns "where_location_id" and "id" are of incompatible types: character varying and integer.
: alter table where_keywords add constraint where_keyword foreign key (where_location_id) references 
        where_locations(id) on delete cascade

Upvotes: 1

Views: 1166

Answers (1)

mu is too short
mu is too short

Reputation: 434965

The error message is fairly clear:

Key columns "where_location_id" and "id" are of incompatible types: character varying and integer

You're creating the where_keywords.where_location_id column as a varchar when it needs to be an integer so that it can refer to where_locations.id in the FK. Your migration has something like this:

create_table :where_keywords do |t|
  #...
  t.string :where_location_id
  #...
end

that should be more like this:

create_table :where_keywords do |t|
  #...
  t.integer :where_location_id
  #...
end

Upvotes: 3

Related Questions