· 8 years ago · Apr 13, 2018, 11:06 PM
1##Old SQL
2drop table if exists recipes;
3drop table if exists categories;
4create table categories (
5 id int not null auto_increment,
6 name varchar(100) not null default '',
7 primary key(id)
8) engine=InnoDB;
9
10create table recipes (
11 id int not null auto_increment,
12 category_id int not null,
13 title varchar(100) not null default '',
14 description varchar(255) null,
15 date date null,
16 instructions text null,
17 constraint fk_recipes_categories foreign key (category_id) references categories(id),
18 primary key(id)
19) engine=InnoDB;
20
21##Ruby migrations file
22class CreateDatabaseTable1 < ActiveRecord::Migration
23 def self.up
24 create_table "categories" do |t|
25 t.column "name", :varchar(100)
26 end
27 create_table "recipes" do |t|
28 t.column 'category_id', :int
29 t.column 'title', :varchar(100)
30 t.column 'description', :varchar(255)
31 t.column 'date', :date
32 t.column 'instructions',:text
33 end
34
35 end
36
37 def self.down
38 drop table "categories";
39 drop table "recipes";
40 end
41end
42
43Correct??