I have a rails DB and I noticed some save errors when putting a lot of text into text areas.
I checked and for some reason they've been created as either text or varchar but with limits of 255, here's the viewer from a DB client.
I tried the below migration to see if it would change to text with no limit however it's not had any effect:
change_column :investors, :notes, :text
change_column :investors, :has_property_notes, :text
change_column :investors, :jv_partner_notes, :text
Other text fields don't seem to have this problem. Any suggestions would be greatly appreciated!
Thanks
Nick
Use limit nil for stubborn databases. Also you should put this in a new migration.
change_column :investors, :notes, :text, :limit => nil
change_column :investors, :has_property_notes, :text, :limit => nil
change_column :investors, :jv_partner_notes, :text, :limit => nil
Related
I had a table posts with column content of string type(255), when i migrate changes,to change type of string to text,it really change that type,but i get text(255) what does nothing.What im must to do to get such result :
TINYTEXT, TEXT, MEDIUMTEXT, LONGTEXT2 | :limit => 1 to 4294967296 (default = 65536)2
p.s. on my localmachine i can create posts of any long and type of string,but on heroku i get
PG::Error: ERROR: value is too long for type character variyng(255)
my _change_datatypes_on_posts_from_string_to_text.rb
class ChangeDatatypeOnPostsFromStringToText < ActiveRecord::Migration
def change
change_column :posts, :content, :text
change_column :posts, :title, :text
change_column :users, :name, :text
end
end
Try using the limit attribute...
eg : "change_column :posts, :content, :text, limit: nil".
On Postgres, a :string, limit:nil is effectively synonymous with :text.
I have the following Rails migration:
class AddTextToReference < ActiveRecord::Migration
def change
add_column :references, :source_text, :text
end
end
I now realise that :source_text also needs to be set to :limit => nil.
How can I add :source_text to my schema without losing the data stored in my :source_text column?
I am using PostgreSQL locally and for production on Heroku.
try change_column method
def change
change_column :references, :source_text, :text, :limit => nil
end
How do you set a default value in a decimal column in Rails? I've tried both the following, using Rails 3 and Postgresql, but after each one the console tells me the default values are still nil. If I set the value from the console, there's no problem, but it doesn't seem to work in the migration.
#Attempt 1
add_column :providers, :commission, :decimal, :precision=>6,:scale=>4,:default=>0.1
and
#Attempt 2
add_column :providers, :commission, :decimal, :precision=>6,:scale=>4,:default=>BigDecimal("0.1")
Many thanks for your help!
It turns out I also need to set :null=>false
The following code worked:
add_column :providers, :commission, :decimal, :precision=>6,:scale=>4,:default=>0.1, :null => false
I am using heroku for a RoR application and am trying to manually set the length of a string column and am having trouble.
I tried making a migration along the lines of
change_column :posts, :content, :string, :length => 10000
I assumed this would work but no such luck, anyone have some pointers?
Thanks!
The length limit option in Rails migrations is called :limit:
change_column :posts, :content, :string, :limit => 10000
If you are finding yourself changing VARCHAR length limits a lot, you might want to read #depesz's blog post on VARCHAR vs TEXT.
I have created a new table including a column "note". The default is varchar(255) I believe but I wish to have this column be a text area vs. a field and to allow more data. I imagine that I would make this change in ActiveRecord::Migration file but I am curious as to the format. Do I simply change the varchar(255) to varchar(1000) for example? (if so what is the format?
def self.up
create_table :notes do |t|
t.string :note :varchar(1000)
end
Is that the right format? Furthermore, how do I get the entry field to be multiple rows. Sorry if this is easy stuff but I am new to programming and RoR. Thanks.
The correct format would be
t.string :note, :limit => 1000
make sure you are using a version of MySQL(or whichever database) which supports varchars longer than 256 characters.
if you want to use a large text block it would be
t.text :note
See http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/TableDefinition.html for more information
You can change the length with the limit option as so...
def self.up
change_column :notes, :note, :string, :limit => 1000
end
You can simply use the 'text' type instead of 'string'.
def self.up
create_table :notes do |t|
t.text :note
end
end
Using the 'text' type will result in database column of type TEXT. Varchar is usually limited to a maximum length of 255 (in MySQL, other RDBMSs have similar limits).
If you use Rails' form helpers, a textarea will be output for this field (because it is of type 'text'). textarea is the form element that accepts multi-line input.
Edit: If you've already migrated the create_table, you can create a new migration to change the column type:
def self.up
change_column :notes, :note, :text
end
Since I had a lot of data already stored I used
self.up
change_column :notes, :note, :text, :limit => nil
end
If I left off the :limit => nil option then the column type would change from varchar to text, but it still had a max length of 255 characters.