質問編集履歴

1

回答に対しての実行結果を追記

2023/09/11 08:30

投稿

stimlocy
stimlocy

スコア11

test CHANGED
File without changes
test CHANGED
@@ -52,3 +52,21 @@
52
52
  [【Rails】主キーのidをオートインクリメントさせないマイグレーションファイル](https://takagi.blog/how-to-write-migration-file-that-does-not-auto-increment-in-rails/)
53
53
  [Rails5でMySQLのid以外にbigint + primary key + auto increment する。(migration)](https://qiita.com/syany/items/1af52fa088e63e0baa7e)
54
54
 
55
+
56
+
57
+ ### 追記:回答1とその実行結果
58
+ 参考:[Rails + mysql でテーブルのidのauto incrementをやめる](https://qiita.com/qsona/items/c3b3ab5ad3a6326f646f)
59
+
60
+ ```
61
+ change_column :users, :id, 'int(11) PRIMARY KEY'
62
+ # => SQLException: table "users" has more than one primary key
63
+ ```
64
+ ```
65
+ remove_column :users, :id
66
+ add_column :users, :id, 'int(11) PRIMARY KEY'
67
+ # => SQLException: Cannot add a PRIMARY KEY column
68
+ ```
69
+
70
+ remove_column と add_column を別々のマイグレーションファイルに分け
71
+ remove_column 実行直後にsqliteからusersテーブルのスキーマを確認した際には、idカラムが消えていたことは確認しています。
72
+ `int(11)`ではなく`BIGINT`の場合も同様でした。