質問編集履歴

2

schema.rbの追記

2021/02/17 10:21

投稿

pay_561
pay_561

スコア26

test CHANGED
File without changes
test CHANGED
@@ -100,6 +100,170 @@
100
100
 
101
101
  end
102
102
 
103
-
104
-
105
- ```
103
+ ```
104
+
105
+ ##schema.rb
106
+
107
+ ```ここに言語を入力
108
+
109
+ # This file is auto-generated from the current state of the database. Instead
110
+
111
+ # of editing this file, please use the migrations feature of Active Record to
112
+
113
+ # incrementally modify your database, and then regenerate this schema definition.
114
+
115
+ #
116
+
117
+ # This file is the source Rails uses to define your schema when running `bin/rails
118
+
119
+ # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
120
+
121
+ # be faster and is potentially less error prone than running all of your
122
+
123
+ # migrations from scratch. Old migrations may fail to apply correctly if those
124
+
125
+ # migrations use external dependencies or application code.
126
+
127
+ #
128
+
129
+ # It's strongly recommended that you check this file into your version control system.
130
+
131
+
132
+
133
+ ActiveRecord::Schema.define(version: 2021_02_15_175631) do
134
+
135
+
136
+
137
+ create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
138
+
139
+ t.string "name", null: false
140
+
141
+ t.string "record_type", null: false
142
+
143
+ t.bigint "record_id", null: false
144
+
145
+ t.bigint "blob_id", null: false
146
+
147
+ t.datetime "created_at", null: false
148
+
149
+ t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
150
+
151
+ t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
152
+
153
+ end
154
+
155
+
156
+
157
+ create_table "active_storage_blobs", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
158
+
159
+ t.string "key", null: false
160
+
161
+ t.string "filename", null: false
162
+
163
+ t.string "content_type"
164
+
165
+ t.text "metadata"
166
+
167
+ t.string "service_name", null: false
168
+
169
+ t.bigint "byte_size", null: false
170
+
171
+ t.string "checksum", null: false
172
+
173
+ t.datetime "created_at", null: false
174
+
175
+ t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
176
+
177
+ end
178
+
179
+
180
+
181
+ create_table "active_storage_variant_records", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
182
+
183
+ t.bigint "blob_id", null: false
184
+
185
+ t.string "variation_digest", null: false
186
+
187
+ t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
188
+
189
+ end
190
+
191
+
192
+
193
+ create_table "posts", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
194
+
195
+ t.string "thumbnail"
196
+
197
+ t.string "title"
198
+
199
+ t.integer "version"
200
+
201
+ t.integer "code"
202
+
203
+ t.text "description"
204
+
205
+ t.datetime "created_at", precision: 6, null: false
206
+
207
+ t.datetime "updated_at", precision: 6, null: false
208
+
209
+ end
210
+
211
+
212
+
213
+ create_table "profiles", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
214
+
215
+ t.bigint "user_id", null: false
216
+
217
+ t.string "name"
218
+
219
+ t.text "description"
220
+
221
+ t.datetime "created_at", precision: 6, null: false
222
+
223
+ t.datetime "updated_at", precision: 6, null: false
224
+
225
+ t.index ["user_id"], name: "index_profiles_on_user_id"
226
+
227
+ end
228
+
229
+
230
+
231
+ create_table "users", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
232
+
233
+ t.string "email", default: "", null: false
234
+
235
+ t.string "encrypted_password", default: "", null: false
236
+
237
+ t.string "reset_password_token"
238
+
239
+ t.datetime "reset_password_sent_at"
240
+
241
+ t.datetime "remember_created_at"
242
+
243
+ t.datetime "created_at", precision: 6, null: false
244
+
245
+ t.datetime "updated_at", precision: 6, null: false
246
+
247
+ t.string "name", default: "", null: false
248
+
249
+ t.text "description"
250
+
251
+ t.index ["email"], name: "index_users_on_email", unique: true
252
+
253
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
254
+
255
+ end
256
+
257
+
258
+
259
+ add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
260
+
261
+ add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
262
+
263
+ add_foreign_key "profiles", "users"
264
+
265
+ end
266
+
267
+
268
+
269
+ ```

1

修正

2021/02/17 10:21

投稿

pay_561
pay_561

スコア26

test CHANGED
File without changes
test CHANGED
@@ -8,33 +8,29 @@
8
8
 
9
9
  ```ここに言語を入力
10
10
 
11
- irb(main):003:0> User.destroy_all
11
+ irb(main):001:0> User.destroy_all
12
-
13
-
14
12
 
15
13
  User Load (0.7ms) SELECT `users`.* FROM `users`
16
14
 
17
15
  TRANSACTION (0.3ms) BEGIN
18
16
 
19
- ActiveStorage::Attachment Load (5.3ms) SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 1 AND `active_storage_attachments`.`record_type` = 'User' AND `active_storage_attachments`.`name` = 'image' LIMIT 1
17
+ ActiveStorage::Attachment Load (0.6ms) SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 1 AND `active_storage_attachments`.`record_type` = 'User' AND `active_storage_attachments`.`name` = 'image' LIMIT 1
20
18
 
21
- Profile Load (6.1ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 1 LIMIT 1
19
+ Profile Load (0.5ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 1 LIMIT 1
22
20
 
23
- ActiveStorage::Attachment Load (0.6ms) SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 1 AND `active_storage_attachments`.`record_type` = 'Profile' AND `active_storage_attachments`.`name` = 'image' LIMIT 1
21
+ ActiveStorage::Attachment Load (0.5ms) SELECT `active_storage_attachments`.* FROM `active_storage_attachments` WHERE `active_storage_attachments`.`record_id` = 1 AND `active_storage_attachments`.`record_type` = 'Profile' AND `active_storage_attachments`.`name` = 'image' LIMIT 1
24
22
 
25
- ActiveStorage::Attachment Destroy (0.6ms) DELETE FROM `active_storage_attachments` WHERE `active_storage_attachments`.`id` = 20
23
+ ActiveStorage::Attachment Destroy (1.0ms) DELETE FROM `active_storage_attachments` WHERE `active_storage_attachments`.`id` = 20
26
24
 
27
- Profile Destroy (0.5ms) DELETE FROM `profiles` WHERE `profiles`.`id` = 1
25
+ User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`profile_id` = 1 LIMIT 1
28
26
 
29
- User Destroy (1.9ms) DELETE FROM `users` WHERE `users`.`id` = 1
30
-
31
- TRANSACTION (2.7ms) ROLLBACK
27
+ TRANSACTION (2.6ms) ROLLBACK
32
28
 
33
29
  Traceback (most recent call last):
34
30
 
35
- 1: from (irb):3
31
+ 1: from (irb):1
36
32
 
37
- ActiveRecord::InvalidForeignKey (Mysql2::Error: Cannot delete or update a parent row: a foreign key constraint fails (`app_development`.`profiles`, CONSTRAINT `fk_rails_e424190865` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`)))
33
+ ActiveRecord::StatementInvalid (Mysql2::Error: Unknown column 'users.profile_id' in 'where clause')
38
34
 
39
35
  ```
40
36
 
@@ -67,6 +63,12 @@
67
63
  validates :name, presence: true
68
64
 
69
65
  validates :description, presence: true
66
+
67
+
68
+
69
+ has_one :user, dependent: :destroy
70
+
71
+
70
72
 
71
73
  end
72
74