I'm having a migration that looks like this
class CreateQuestionings < ActiveRecord::Migration[5.0]
def change
create_table :questionings do |t|
t.text :body, null: false, limit: 260
end
end
end
Now when I run $ rake db:migrate:reset
the limit is nowhere to be seen in my db/schema.rb
:
create_table "questionings", force: :cascade do |t|
t.text "body", null: false
end
Am I doing it wrong or is this a bug?
By the way, I am using rails 5.0.0.beta3 and ruby 2.3.0p0.
The change method can be used to drop a column in Rails 4 applications, but should not be used in Rails 3. I updated my answer accordingly. You can also use remove_column :table_name, :column_name, :type, :options within the change method, since if you specify the type reverting the migration is possible.
An index is used to speed up the performance of queries on a database. Rails allows us to create index on a database column by means of a migration. By default, the sort order for the index is ascending. But consider the case where we are fetching reports from the database.
t.text
produces a text
column in PostgreSQL and text
doesn't allow for size limits because text
is:
variable unlimited length
Since there's no limit supported by the database, the PostgreSQL driver won't look for a :limit
option; keep in mind that you're saying t.text(column_name, options_hash)
so you can throw whatever you want into options_hash
and the driver will ignore anything that it isn't specifically looking for.
If you want to limit the column size then you can either manually add a CHECK constraint (which ActiveRecord won't understand so you'll have to switch from schema.rb
to structure.sql
) or use a varchar
column (AKA t.string
):
t.string :body, null: false, limit: 260
Also, your schema.rb
is generated based on what is in the database, not what's in your migrations. Since text
doesn't support a limit, the database won't know about your limit: 260
option; if the database doesn't know about it, ActiveRecord won't get it back from the database when ActiveRecord is asking the database for schema information.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With