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
Related
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
When I push my rails 4 app to Heroku, I receive the following fatal rake error:
You're trying to register ActiveAdmin::Comment as Comment, but the existing ActiveAdmin::Resource config was built for Comment!
I have a Comment model which has an ActiveAdmin file like this:
# /app/admin/comment.rb
ActiveAdmin.register Comment do
end
An ActiveAdmin migration creates the following active_admin_comments table. I am wondering if this might be related to the conflict, but I am not sure.
class CreateActiveAdminComments < ActiveRecord::Migration
def self.up
create_table :active_admin_comments do |t|
t.string :namespace
t.text :body
t.string :resource_id, :null => false
t.string :resource_type, :null => false
t.references :author, :polymorphic => true
t.timestamps
end
add_index :active_admin_comments, [:namespace]
add_index :active_admin_comments, [:author_type, :author_id]
add_index :active_admin_comments, [:resource_type, :resource_id]
end
def self.down
drop_table :active_admin_comments
end
end
I did have ActiveAdmin working fine on Heroku before this, including both of the files above. I cannot think of what has changed since the last push that could affect the Comment model. Any points of direction would be greatly appreciated!
I've been working on a Rails 4.0 application with sqlite (default for Rails development environment) for events (hackathons) which has a parent model, Event, for which there can be many Press_Blurbs.
First I ran some scaffolding generators which created some migrations that I ran seemingly without issue:
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.string :city
t.string :theme
t.datetime :hackathon_start
t.datetime :hackathon_end
t.datetime :show_start
t.datetime :show_end
t.text :about
t.string :hack_rsvp_url
t.string :show_rsvp_url
t.timestamps
end
end
end
class CreatePressBlurbs < ActiveRecord::Migration
def change
create_table :press_blurbs do |t|
t.string :headline
t.string :source_name
t.string :source_url
t.string :logo_uri
t.timestamps
end
end
end
Then I added some relationships to the models:
class Event < ActiveRecord::Base
has_many :press_blurbs
end
class PressBlurb < ActiveRecord::Base
belongs_to :event
end
...and added/ran a migration to add a table reference:
class AddEventRefToPressBlurbs < ActiveRecord::Migration
def change
add_column :press_blurbs, :event, :reference
end
end
Nevertheless, when I look at schema.db this is what I see instead of tables definitions:
# Could not dump table "events" because of following NoMethodError
# undefined method `[]' for nil:NilClass
# Could not dump table "press_blurbs" because of following NoMethodError
# undefined method `[]' for nil:NilClass
Other unrelated tables show up in schema.rb perfectly fine, but these do not. Any idea what's going on?
I think your last migration is wrong. I would change it to this:
class AddEventRefToPressBlurbs < ActiveRecord::Migration
def change
add_reference :press_blurb, :event
end
end
Unfortunately, your database is likely in a wonky state because it has an invalid column type (i.e. there is no 'reference' column type, but sqlite made it anyway). Hopefully, it's just a development database so you can just delete it and start fresh.
Just in case this helps someone. I was doing this on a sqlite dev db, and as mentioned above, it was likely in a wonky state from all my experimental migrations. By deleting the sqlite file, recreating and running all the migrations, it is now fine.
My sqlite3 database works fine in development but when I try to migrate it to production I get the following error:
PG::Error: ERROR: relation "movies" does not exist
: ALTER TABLE "movies" ADD COLUMN "production_company" character varying(255)/app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.2.12/lib/active_record/connection_adapters/postgresql_adapter.rb:652:in `async_exec'
I know a few people have posted about this but nothing I've tried seems to work. Anyone know how I might fix this?
Here's the migration:
class AddProductionCompanyToMovies < ActiveRecord::Migration
def change
add_column :movies, :production_company, :string, :limit => nil
end
end
Here's my schema.rb file if this helps:
ActiveRecord::Schema.define(:version => 20130331014529) do
create_table "movies", :force => true do |t|
t.string "title"
t.string "actor_1"
t.string "locations"
t.string "release_year"
t.string "string"
t.string "actor_2"
t.string "actor_3"
t.string "writer"
t.string "director"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.string "production_company"
t.string "distributor"
t.string "fun_facts"
end
end
Here's the migration where I create the movies table:
class Movies < ActiveRecord::Migration
def up
end
def down
end
end
It's not the best approach but a quick fix would be to replace that migration with this:
class AddProductionCompanyToMovies < ActiveRecord::Migration
def change
create_table :movies do |t|
t.string :production_company
t.timestamps
end
end
end
Your migration where you create the movie table is incorrect. The up and down methods you have don't do anything. Because of this, there is no movie table to add the production_company column to.
You need something like this;
class Movies < ActiveRecord::Migration
def change
create_table :movies do |t|
t.string :title
t.string :actor
.
. #add your columns you want in your initial migration here
.
end
end
I can't say why things worked in development in SQLite but at some point you successfully created the movies table and then maybe you altered the migration after that. This is easy to do (I've done it!).
A lot of people recommend that when you set up production you don't run your migrations to set up the database, but instead use rake db:schema:load (in fact, if you read the comments at the top of your db/schema.rb file it specifically describes this).
Another point is that a lot of people recommend having the same database in development as in production as there are subtle differences that can lead to unexpected problems in production (this is not what has caused your issue). If you're only just starting out then don't worry about it for now; it's just one more headache to set up PostgreSQL on your own machine; but it's something to keep in mind as you progress.
I jus created a rails application. I created a model using ruby script/generate model Article
next i edited the my 001_create_articles.rb file by adding these lines in self.up method
def self.up
create_table :articles do |t|
t.string :title
t.text :body
t.string :published_at
t.timestamps
end
end
Now i ran rake db:migrate . But migrate does not work, it simply does no print anything. Anyone knows where i am going wrong?
Are you missing one end from your up method?
def self.up
create_table :articles do |t|
t.string :title
t.text :body
t.string :published_at
t.timestamps
end
end
I think you have to generate a migration. If i understood it right, you added the migration code to the model.
You have to run something like that:
ruby script/generate migration articles
After that open the generated file and add your code there. Hope it helps