Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is :cascade in rails schema.rb and where did it come from?

After adding another migration and occasional decision to drop and migrate I checked my schema.rb and saw this

create_table "users", force: :cascade do |t| 

I haven't committed this changes yet and on remote I have this

create_table "users", force: true do |t| 

Now I have cascade in front of each table. What is cascade and where did it come from?

like image 433
kirqe Avatar asked Dec 29 '14 22:12

kirqe


People also ask

How is schema rb generated?

It is a Ruby representation of your database; schema. rb is created by inspecting the database and expressing its structure using Ruby.

What is schema rb in rails?

The schema. rb serves mainly two purposes: It documents the final current state of the database schema. Often, especially when you have more than a couple of migrations, it's hard to deduce the schema just from the migrations alone. With a present schema.

How do I roll back migration in Rails?

You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.

What are migrations in rails?

A Rails migration is a tool for changing an application's database schema. Instead of managing SQL scripts, you define database changes in a domain-specific language (DSL). The code is database-independent, so you can easily move your app to a new platform.


2 Answers

The docs explain what :cascade does:

:force - Set to :cascade to drop dependent objects as well. Defaults to false.

One reason you may be seeing this is a change in Rails 4.2 in SchemaDumper to use :cascade, release notes.

Release notes about change:

SchemaDumper uses force: :cascade on create_table. This makes it possible to reload a schema when foreign keys are in place.

like image 190
Andrew Hubbs Avatar answered Sep 30 '22 22:09

Andrew Hubbs


http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-create_table

:force Set to true to drop the table before creating it. Set to :cascade to drop dependent objects as well. Defaults to false.

like image 29
Lane Avatar answered Sep 30 '22 23:09

Lane