Получение следующей ошибки при миграции - PullRequest
1 голос
/ 20 февраля 2012

Я работаю над проектом rails 2 и получаю следующую ошибку при выполнении задач rake. Может ли кто-нибудь помочь мне, что может быть причиной этого.

[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

1 Ответ

3 голосов
/ 20 февраля 2012

Сообщение об ошибке довольно ясно:

Ключевые столбцы "where_location_id" и "id" имеют несовместимые типы: переменные и целые числа

Вы создаете столбец where_keywords.where_location_id как varchar, когда он должен быть integer, чтобы он мог ссылаться на where_locations.id в FK. Ваша миграция имеет что-то вроде этого:

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

это должно быть больше похоже на это:

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