class CreateTodos < ActiveRecord::Migration[6.0]
def change
create_table :todos do |t|
t.string :title
t.string :created_by
t.timestamps
end
end
end
rails g model Item name:string done:boolean todo:references
class CreateItems < ActiveRecord::Migration[6.0]
def change
create_table :items do |t|
t.string :name
t.boolean :done
t.references :todo, null: false, foreign_key: true
t.timestamps
end
end
end
migration
rails db:migration
mysql> desc items;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint | NO | PRI | NULL | auto_increment |
| name | varchar(255) | YES | | NULL | |
| done | tinyint(1) | YES | | NULL | |
| todo_id | bigint | NO | MUL | NULL | |
| created_at | datetime(6) | NO | | NULL | |
| updated_at | datetime(6) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)
mysql> desc todos;
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | bigint | NO | PRI | NULL | auto_increment |
| title | varchar(255) | YES | | NULL | |
| created_by | varchar(255) | YES | | NULL | |
| created_at | datetime(6) | NO | | NULL | |
| updated_at | datetime(6) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
5 rows in set (0.00 sec)
https://qiita.com/yyh-gl/items/30bd91c2b33fdfbe49b5#%E3%83%A2%E3%83%87%E3%83%AB%E3%81%AE%E4%BD%9C%E6%88%90
railsのmigrationを使ってテーブル構築を実施。
モデル作成とmigrationファイル生成
migration
id、created_at、updated_atが勝手に作られてる。
https://www.atotok.co.jp/labo/b61db7988b5e22bdda10804d4855dc58 発行したmigrationファイルを修正すればいいらしい。