End chapter 10
[depot.git] / db / migrate / 20090304153613_create_line_items.rb
index 6870a05a03e9c333977d843f037b0683ab2dd149..fcd4625f33b61a965f9c0f14374d768f44b9f6bd 100644 (file)
@@ -1,13 +1,20 @@
 class CreateLineItems < ActiveRecord::Migration
   def self.up
     create_table :line_items do |t|
-      t.integer :product_id
-      t.integer :order_id
-      t.integer :quantity
-      t.decimal :total_price
+      t.integer :product_id,  :null => false
+      t.integer :order_id,    :null => false
+      t.integer :quantity,    :null => false
+      t.decimal :total_price, :null => false, :precision => 8, :scale => 2
 
       t.timestamps
     end
+
+    execute "alter table line_items add constraint fk_line_item_products
+                foreign key (product_id) references products(id)"
+
+    execute "alter table line_items add constraint fk_line_item_orders
+                foreign key (order_id) references orders(id)"
+    
   end
 
   def self.down