This question already has answers here:
PG::StringDataRightTruncation: ERROR: PostgreSQL string(255) limit | Heroku
(3 answers)
Closed 7 years ago.
I have a ruby on rails project that allows me to submit a comment (called a note). It seems to work fine on my computer and I'm using sql or whatever comes stock with rails.
After I put the app on heroku everything is working good except now my notes that are longer that 255 characters return an error.
I'm pretty sure it has to do with the database since on localhost:3000 I can save hundreds of lines of text.
I however don't know anything about databases - can someone point me in the right direction with this error? the error is:
PG::StringDataRightTruncation: ERROR: value too long for type
character varying(255)
Here's the migration for notes, and its defined as a string, not a Character:
class CreateNotes < ActiveRecord::Migration
def change
create_table :notes do |t|
t.string :title
t.string :comment
t.integer :user_id
t.integer :record_id
t.timestamps
end
end
end
You should be using :text field type for attributes that will hold more than 255 characters.
class CreateNotes < ActiveRecord::Migration
def change
create_table :notes do |t|
t.text :title
t.text :comment
t.integer :user_id
t.integer :record_id
t.timestamps
end
end
This will only work if you are able to recreate your database by doing
rake db:drop
rake db:create
rake db:migrate
If you are not in the position to reset the database you will have to do a new migration with
def change
change_column :notes, :title, :text
change_column :notes, :comment, :text
end
Related
Hello I started my rails application using sqlite, however when I tried deploying it on heroku I found out that I needed to use postgreSQL. So I went through the trouble to change my gemfile and database.yml file and create the new postgresql database. However when I try to migrate my database I get the error:
ActiveRecord::StatementInvalid: PG::UndefinedObject: ERROR: type "reference" does not exist
LINE 1: ALTER TABLE "questions" ADD "quiz_id" reference
that is probably because I used a reference to make a relation in my db
I am basically looking for the fix for this situation.
Here are my migrations(if it matters):
class CreateQuestions < ActiveRecord::Migration
def change
create_table :questions do |t|
t.string :question
t.string :answer1
t.string :answer2
t.string :answer3
t.string :answer4
t.integer :correct_id
t.timestamps null: false
end
end
end
class CreateQuizzes < ActiveRecord::Migration
def change
create_table :quizzes do |t|
t.string :name
t.string :subject
t.timestamps null: false
end
end
end
class AddQuizIdToQuestions < ActiveRecord::Migration
def change
add_column :questions, :quiz_id, :reference
end
end
Edit new question:
When on my heroku server there are some dead pages, but not when I am running on my local server. Here is my heroku address: https://krisquiz.herokuapp.com/
The dead pages are when you submit a question and when you try and start a quiz. I looked at the urls and they look properly. The only thing in common that I can think of for the two pages is that I built the urls manually for the links (ex: request.base_url + '/quiz/' + quiz.id.to_s + '/start'). As I am not sure what I need to give you as information just tell me and I will try to quickly get back to you.
Use integer type instead of references
add_column :questions, :quiz_id, :integer
I was only able to discouver this problem by slowly walking through running my migrations. For the last two days I've been trying to do somthing simple. Remove one column verdict:text from my simulations table and add another column: opinions:hash.
Doing this caused a variety of errors, such as no method 'my_sym' and saying the simulation table didn't exist.
I initially thought I had resolved this by doing:
rake db:drop
rake db:schema:dump
rake db:migrate VERSION="<Migration1>"
rake db:migrate VERSION="<Migration2>"
rake db:setup ENV="test"
Migration 1:
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Database authenticatable
t.string :email, :null => false, :default => "", limit: 96
t.string :encrypted_password, :null => false, :default => "", limit: 60
t.timestamps
t.index :email, unique: true
end
end
end
Migration 2:
class CreateSimulations < ActiveRecord::Migration
def change
# Needs the hash column worked out before this is run
create_table :simulations do |t|
t.integer :x_size
t.integer :y_size
t.string :verdict
t.string :arrangement
end
add_reference :simulations, :user, index: true
end
end
This got me back to square one (all my tests worked everything seemed to be in my initial state I had before started having problems), so I re-wrote the two offending migrations thinking they might be the problem, ran the two migrations in this order (I removed the column then added the other one, the opposite order of what I tried previous, thinking maybe the order had some effect).
Migration 3:
class RemoveVerdictFromSimulation < ActiveRecord::Migration
def change
remove_column :simulations, :verdict, :string
end
end
Migrations 4:
class AddOpinionToSimulations < ActiveRecord::Migration
def change
add_column :simulations, :opinion, :hash
end
end
Edit: With further investigation it is this 4th migration that is causing the problem of dropping the simulations table without error.
Both these migrations worked without error. However I know doing anything else will cause an error (example running rspec), because it looks like the problem is that one of these migration causes an error with the schema.rb file.
After running these last migrations 3&4 my schema.rb which previously had both users and simulations tables now looks like this:
ActiveRecord::Schema.define(version: 20150807193122) do
# Could not dump table "simulations" because of following NoMethodError
# undefined method `[]' for nil:NilClass
create_table "users", force: :cascade do |t|
t.string "email", limit: 96, default: "", null: false
t.string "encrypted_password", limit: 60, default: "", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
end
The particular line of interest here being:
# Could not dump table "simulations" because of following NoMethodError
# undefined method `[]' for nil:NilClass
Can anyone shed some light on whats happening here or how I can fix it? I've been stuck on this for hours.
One thing maybe worth noting, when I re-run the rake db:schema:dump I need too comment out my factories for some reason, as they seem to cause errors with simulation table existing. Not sure why they're being called just thought this info might help, here they both are:
FactoryGirl.define do
factory :simulation do |f|
f.id (Simulation.last.nil? ? 1 : Simulation.last.id + 1)
f.x_size 3
f.y_size 3
f.user_id 1
end
end
--
FactoryGirl.define do
factory :user do
email "user_#{User.last.nil? ? 1 : User.last.id + 1}#home.com"
password "password"
end
end
I think the problem is right here:
class AddOpinionToSimulations < ActiveRecord::Migration
def change
add_column :simulations, :opinion, :hash
end
end
There's no such thing as hash column type. SQLite will, unfortunately, let you create columns with any type you want (with unrecognized things being treated as strings) but ActiveRecord won't know what to do with them. The result is that your migration works but the schema dump fails because ActiveRecord can't say:
t.hash :opinion
in the schema.rb file.
If you really think you want to store a Hash in a column, you'd create a text column:
add_column :simulations, :opinion, :text
and then use serialize in your model:
serialize :opinion, Hash
Of course that leaves you with an opaque blob of YAML in your database that you won't be able to (sanely) query so it should only be used if you are okay with that.
A better solution would be to normalize your opinion Hashes so that you could store their information in separate tables/models.
For example, I have a migration file for posts:
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.text :text
t.integer :ip
t.timestamps
end
end
end
And want to change it to:
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.text :text
t.integer :ip, :limit => 8
t.timestamps
end
end
end
Would I add a line:
change_column :posts, :ip, :limit => 8
Below so that the file is:
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.text :text
t.integer :ip, :limit => 8
t.timestamps
change_column :posts, :ip, :limit => 8
end
end
end
And then run heroku run rake --trace db:migrate
I'm having trouble understanding how migrations work, and especially to production, so any help would be greatly appreciated.
There is http://guides.rubyonrails.org/active_record_migrations.html#changing-columns a section 3.5 on column modifiers but it doesn't specify how to pass them.
Thanks!
You should create a separate migration for adding the limit on the ip column.
Generate your migration:
rails generate migration ChangeIpLimitOnPosts
Inside the generated migration file, update the contents:
class ChangeIpLimitOnPosts < ActiveRecord::Migration
def up
change_column :posts, :ip, :integer, limit: 8
end
def down
change_column :posts, :ip, :integer
end
end
Take note here: you need to specify the column type when changing the column, even though in your case, you're not changing the type.
Also, in this case, Active Record will not know how to reverse the transaction if you need to rollback, so you need to explicitly tell Active Record how to do that — this can be done using the up and down methods, rather than change.
Run your migration:
rake db:migrate
On Heroku:
heroku run rake db:migrate
Hope that helps.
I'm struggling here with the db migration for the acts_as_commentable_with_threading.
After generating the migration rails generate acts_as_commentable_with_threading_migration I proceeded to add the comment table rake db:migrate. Nothing happened, no error message, just returned to the regular prompt.
Looking at other response to this problem I tried rake db:migrate VERSION= # version number.
Yet here I get an Error response ActiveRecord::UnknownMigrationVersionError:
I must be doing something extremely wrong here since the computer doesn't validate the existence of my comment migration...
UPDATE from #Tiago answer
Ran rails generate migration acts_as_commentable_with_threading_migration
I had to manually create the migration by adding this code to the migration file. Then, thedb:migration worked perfectly.
Why wasn't it working in the first place? The Documentation clearly indicate to run rails generate acts_as_commentable_with_threading_migration.
Just create a migration for yourself as indicated in the gem's page:
rails g migration acts_as_commentable_with_threading_migration
And paste that to the file:
class ActsAsCommentableWithThreadingMigration < ActiveRecord::Migration
def self.up
create_table :comments, :force => true do |t|
t.integer :commentable_id, :default => 0
t.string :commentable_type
t.string :title
t.text :body
t.string :subject
t.integer :user_id, :default => 0, :null => false
t.integer :parent_id, :lft, :rgt
t.timestamps
end
add_index :comments, :user_id
add_index :comments, [:commentable_id, :commentable_type]
end
def self.down
drop_table :comments
end
end
I've just started working through the Rails 3 tutorials so that I can develop a bit of familiarity with the framework, but I'm running into issues with the generation of schema.rb. My operating system is Windows 7 x64, Ruby 1.9.2, MySQL2 gem 0.2.6, Rails 3.0.3.
I have two migrations created, one for my lists:
class CreateLists < ActiveRecord::Migration
def self.up
create_table :lists do |t|
t.string :name
t.text :description
t.timestamps
end
end
def self.down
drop_table :lists
end
end
and one for my List items:
class CreateItems < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.string :name
t.string :type
t.boolean :completed
t.references :list
t.timestamps
end
end
def self.down
drop_table :items
end
end
Rake migrates successfully and the application works as expected, but schema.rb shows only:
ActiveRecord::Schema.define(:version => 20101126074332) do
# Could not dump table "items" because of following ArgumentError
# invalid date
# Could not dump table "lists" because of following ArgumentError
# invalid date
Is anyone a bit more experienced with Rails that could offer advice on what might be causing the problem? Googling turned up nothing.
Get the mysql 5.1 libmysql.dll as described in:
https://github.com/brianmario/mysql2/issues#issue/71
Try rake db:schema:dump.