Skip to content

Instantly share code, notes, and snippets.

@kimegede
Last active August 29, 2015 14:17
Show Gist options
  • Save kimegede/3c5775857d9c0ab189d3 to your computer and use it in GitHub Desktop.
Save kimegede/3c5775857d9c0ab189d3 to your computer and use it in GitHub Desktop.
class CreateZipcodes < ActiveRecord::Migration
def change
create_table(:zipcodes, id: false) do |t|
t.integer :zipcode, primary_key: true
t.string :city
t.string :street
t.string :company
t.boolean :provins
t.integer :country
t.timestamps null: false
end
end
end
class CreateResidences < ActiveRecord::Migration
def change
create_table :residences do |t|
t.string :name
t.string :address
t.belongs_to :zipcode, index: true
t.timestamps null: false
end
add_foreign_key :residences, :zipcodes
end
end
Mysql2::Error: Cannot add foreign key constraint: ALTER TABLE `residences` ADD CONSTRAINT `fk_rails_e76d45279d`
FOREIGN KEY (`zipcode_id`)
REFERENCES `zipcodes` (`id`)
@dminca
Copy link

dminca commented Mar 25, 2015

Try create_table :zipcodes do |t|
Without id: false

@kimegede
Copy link
Author

@completit it works now, it was the space after create_table (:zipcodes.... there made the mistake.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment