db/schema.rb là snapshot toàn bộ schema hiện tại — được Rails tự sinh sau mỗi lần db:migrate.
Nó không phải migration, mà là trạng thái cuối cùng của DB.
# db/schema.rb (auto-generated)
ActiveRecord::Schema[7.1].define(version: 2025_05_01_000001) do
create_table "users", force: :cascade do |t|
t.string "email", null: false
t.integer "age"
t.timestamps
end
endTại sao quan trọng:
- rails db:schema:load tạo DB mới từ schema.rb (nhanh hơn replay toàn bộ migrations).
- Dùng để setup DB cho test environment: rails db:test:prepare.
- Commit vào git để PR reviewer thấy schema thay đổi.
Sau khi sửa DB, luôn commit cả migration file lẫn schema.rb cùng nhau.
db/schema.rb is a snapshot of the current full schema — auto-generated by Rails after every db:migrate.
It's not a migration; it's the final state of the DB.
# db/schema.rb (auto-generated)
ActiveRecord::Schema[7.1].define(version: 2025_05_01_000001) do
create_table "users", force: :cascade do |t|
t.string "email", null: false
t.integer "age"
t.timestamps
end
endWhy it matters:
- rails db:schema:load creates a fresh DB from schema.rb (faster than replaying all migrations).
- Used to set up the test DB: rails db:test:prepare.
- Commit it to git so PR reviewers can see what changed in the schema.
After modifying the DB, always commit both the migration file and schema.rb together.