teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

schema.rbの追記

2021/02/17 10:21

投稿

pay_561
pay_561

スコア26

title CHANGED
File without changes
body CHANGED
@@ -49,5 +49,87 @@
49
49
 
50
50
  validates :name, presence: true
51
51
  end
52
+ ```
53
+ ##schema.rb
54
+ ```ここに言語を入力
55
+ # This file is auto-generated from the current state of the database. Instead
56
+ # of editing this file, please use the migrations feature of Active Record to
57
+ # incrementally modify your database, and then regenerate this schema definition.
58
+ #
59
+ # This file is the source Rails uses to define your schema when running `bin/rails
60
+ # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
61
+ # be faster and is potentially less error prone than running all of your
62
+ # migrations from scratch. Old migrations may fail to apply correctly if those
63
+ # migrations use external dependencies or application code.
64
+ #
65
+ # It's strongly recommended that you check this file into your version control system.
52
66
 
67
+ ActiveRecord::Schema.define(version: 2021_02_15_175631) do
68
+
69
+ create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
70
+ t.string "name", null: false
71
+ t.string "record_type", null: false
72
+ t.bigint "record_id", null: false
73
+ t.bigint "blob_id", null: false
74
+ t.datetime "created_at", null: false
75
+ t.index ["blob_id"], name: "index_active_storage_attachments_on_blob_id"
76
+ t.index ["record_type", "record_id", "name", "blob_id"], name: "index_active_storage_attachments_uniqueness", unique: true
77
+ end
78
+
79
+ create_table "active_storage_blobs", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
80
+ t.string "key", null: false
81
+ t.string "filename", null: false
82
+ t.string "content_type"
83
+ t.text "metadata"
84
+ t.string "service_name", null: false
85
+ t.bigint "byte_size", null: false
86
+ t.string "checksum", null: false
87
+ t.datetime "created_at", null: false
88
+ t.index ["key"], name: "index_active_storage_blobs_on_key", unique: true
89
+ end
90
+
91
+ create_table "active_storage_variant_records", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
92
+ t.bigint "blob_id", null: false
93
+ t.string "variation_digest", null: false
94
+ t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
95
+ end
96
+
97
+ create_table "posts", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
98
+ t.string "thumbnail"
99
+ t.string "title"
100
+ t.integer "version"
101
+ t.integer "code"
102
+ t.text "description"
103
+ t.datetime "created_at", precision: 6, null: false
104
+ t.datetime "updated_at", precision: 6, null: false
105
+ end
106
+
107
+ create_table "profiles", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
108
+ t.bigint "user_id", null: false
109
+ t.string "name"
110
+ t.text "description"
111
+ t.datetime "created_at", precision: 6, null: false
112
+ t.datetime "updated_at", precision: 6, null: false
113
+ t.index ["user_id"], name: "index_profiles_on_user_id"
114
+ end
115
+
116
+ create_table "users", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t|
117
+ t.string "email", default: "", null: false
118
+ t.string "encrypted_password", default: "", null: false
119
+ t.string "reset_password_token"
120
+ t.datetime "reset_password_sent_at"
121
+ t.datetime "remember_created_at"
122
+ t.datetime "created_at", precision: 6, null: false
123
+ t.datetime "updated_at", precision: 6, null: false
124
+ t.string "name", default: "", null: false
125
+ t.text "description"
126
+ t.index ["email"], name: "index_users_on_email", unique: true
127
+ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
128
+ end
129
+
130
+ add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
131
+ add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
132
+ add_foreign_key "profiles", "users"
133
+ end
134
+
53
135
  ```

1

修正

2021/02/17 10:21

投稿

pay_561
pay_561

スコア26

title CHANGED
File without changes
body CHANGED
@@ -3,20 +3,18 @@
3
3
 
4
4
  ##エラー
5
5
  ```ここに言語を入力
6
- irb(main):003:0> User.destroy_all
6
+ irb(main):001:0> User.destroy_all
7
-
8
7
  User Load (0.7ms) SELECT `users`.* FROM `users`
9
8
  TRANSACTION (0.3ms) BEGIN
10
- 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
11
- Profile Load (6.1ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 1 LIMIT 1
12
- 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
13
- ActiveStorage::Attachment Destroy (0.6ms) DELETE FROM `active_storage_attachments` WHERE `active_storage_attachments`.`id` = 20
14
- Profile Destroy (0.5ms) DELETE FROM `profiles` WHERE `profiles`.`id` = 1
15
- User Destroy (1.9ms) DELETE FROM `users` WHERE `users`.`id` = 1
16
- TRANSACTION (2.7ms) ROLLBACK
9
+ 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
10
+ Profile Load (0.5ms) SELECT `profiles`.* FROM `profiles` WHERE `profiles`.`user_id` = 1 LIMIT 1
11
+ 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
12
+ ActiveStorage::Attachment Destroy (1.0ms) DELETE FROM `active_storage_attachments` WHERE `active_storage_attachments`.`id` = 20
13
+ User Load (0.9ms) SELECT `users`.* FROM `users` WHERE `users`.`profile_id` = 1 LIMIT 1
14
+ TRANSACTION (2.6ms) ROLLBACK
17
15
  Traceback (most recent call last):
18
- 1: from (irb):3
19
- 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`)))
16
+ 1: from (irb):1
17
+ ActiveRecord::StatementInvalid (Mysql2::Error: Unknown column 'users.profile_id' in 'where clause')
20
18
  ```
21
19
  ##post.rb
22
20
  ```ここに言語を入力
@@ -33,6 +31,9 @@
33
31
 
34
32
  validates :name, presence: true
35
33
  validates :description, presence: true
34
+
35
+ has_one :user, dependent: :destroy
36
+
36
37
  end
37
38
  ```
38
39
  ##user.rb