I am trying to setup a Rails app on Heroku using PG, and also using devise and oauth to interact with the Yahoo Sports api.
I am running into an issue where when the users goes to authenticate the Heroku logs are telling me:
ActiveRecord::StatementInvalid (PG::Error: ERROR: value too long for type character varying(255)
So after reading this Stack Overflow post PostgreSQL string(255) limit - Rails, Ruby and Heroku, I tried changing the token to text from string since that seemed to be the longest thing that may be causing the over 255 limit. My migration looked like this:
class AddAccessTokenToAuthentications < ActiveRecord::Migration
def change
add_column :authentications, :token, :text, :limit => nil
add_column :authentications, :secret, :string
end
end
However for some reason it seems to be changing everything to text. When I look at my schema after migrating it shows:
create_table "authentications", :force => true do |t|
t.integer "user_id"
t.text "provider", :limit => 255
t.text "uid", :limit => 255
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.text "token", :limit => 255
t.text "secret", :limit => 255
end
When I push to Heroku and try to migrate on Heroku I am getting this error:
-- create_table("authentications", {:force=>true})
NOTICE: CREATE TABLE will create implicit sequence "authentications_id_seq" for serial column "authentications.id"
rake aborted!
PG::Error: ERROR: type modifier is not allowed for type "text"
LINE 1: ...serial primary key, "user_id" integer, "provider" text(255),...
: CREATE TABLE "authentications" ("id" serial primary key, "user_id" integer, "provider" text(255), "uid" text(255), "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, "token" text(255), "secret" text(255))
I am not sure how to resolve this or what I should be looking into.
PG::Error: ERROR: type modifier is not allowed for type "text"
means you can't use :limit
What you can do is either:
t.text "provider" # = varchar(max)
or
t.string "provider", :limit => 2147483647 # for max limit based on your db
Your text fields are unable to have a limit on them. Perhaps you meant to use string instead?
Related
Everytimes, I have a column for my event object (event-calendar gem) in my schema.rb file
t.integer "id", :null => false
So of course, every time I want to create an event I have an error "null value in column "id" violates not-null constraint" because of this. I tried this solution ActiveRecord::StatementInvalid: PG::Error: ERROR: null value in column "id" violates not-null constraint but it appears everytimes !!! I would like to know WHY this column appears here, I really don't understand...
Any idea ?
When you created the events model, it sounds like you added an id field. This isn't necessary as rails automatically creates this field. Your event model in your schema should look something like:
create_table "events", :force => true do |t|
t.string "name"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
I would rollback the migration, assuming this is the most recent migration:
rake db:rollback
Then update the migration file and remove the id from the create_table block:
class CreateEvents < ActiveRecord::Migration
def change
create_table :events do |t|
t.string :name
t.timestamps
end
end
end
Then rerun the migration.
I am trying to run this migration:
class RemoveClientFromSalesteam < ActiveRecord::Migration
change_table :sales_teams do |t|
t.remove :client_id
end
end
This is the error I am getting:
rake db:migrate
-- change_table(:sales_teams)
rake aborted!
An error has occurred, this and all later migrations canceled:
Index name 'temp_index_altered_sales_teams_on_client_priority_and_personal_priority' on table 'altered_sales_teams' is too long; the limit is 64 characters
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
This is what my schema.rb looks like:
create_table "sales_teams", :force => true do |t|
t.string "name"
t.integer "firm_id"
t.boolean "client_priority"
t.boolean "personal_priority"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "client_id"
end
add_index "sales_teams", ["client_id"], :name => "index_sales_teams_on_client_id"
add_index "sales_teams", ["client_priority", "personal_priority"], :name => "index_sales_teams_on_client_priority_and_personal_priority"
add_index "sales_teams", ["name", "firm_id"], :name => "index_sales_teams_on_name_and_firm_id"
Thoughts?
Thanks.
Drop the index, remove your column, and then re-add the index:
def up
remove_index :sales_teams, :column => [ :client_priority, :personal_priority ]
remove_column :sales_teams, :client_id
add_index :sales_teams, [ :client_priority, :personal_priority ]
end
I'm guessing that you're using SQLite, most databases support real ALTER TABLE operations for removing columns but SQLite forces you to copy the table (and indexes), drop the table, and copy everything back; the Rails SQLite driver takes care of this behind the scenes but, apparently, doesn't know about the identifier length limit.
You can also specify your own index names by using the :name option to add_index and remove_index if necessary.
I just deployed a simple Ruby on Rails (3.0.10) application to Heroku. There is a line of code create a new User object like this.
User.create(:name => "[name here]", :email => "[email here]")
It can run well in my local machine, which is using MySQL. After I deployed it to Heroku, I got an error.
ActiveRecord::StatementInvalid: PGError: ERROR: null value in column "id" violates not-null constraint : INSERT INTO "users" ("id", "name", "email", "created_at", "updated_at") VALUES (NULL, '[name here]', '[email here]', '2011-09-28 03:59:12.908593', '2011-09-28 03:59:12.908593') RETURNING "id"
I have no idea what's wrong with my code. Did i miss anything?
Thanks all.
UPDATE
Migration
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :email
t.string :name
t.timestamps
end
end
def self.down
drop_table :users
end
end
Schema
ActiveRecord::Schema.define(:version => 20110922071106) do
create_table "users", :force => true do |t|
t.string "email"
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
end
Did you do this:
(Taken from http://railsapps.github.com/rails-heroku-tutorial.html)
Replace SQLite with PostgreSQL If you are developing locally using
SQLite, you will need to switch to PostgreSQL for deployment to
Heroku. If you don’t want to develop using PostgreSQL locally, you
can set up your Gemfile to use SQLite for development and PostgreSQL
for production.
To switch from SQLite to PostgreSQL for deployment to Heroku, edit
your Gemfile and change this line:
gem 'sqlite3'
To this:
group :production do
gem 'pg'
end
group :development, :test do
gem 'sqlite3'
end
For anyone else looking, I had this same problem. Worked fine in dev, but failed in production with Postgres. The problem I had I tracked back to the CanCan plugin, specifically code I had that was creating a guest user the ability.rb file, if no user object existed. The guest account was suggested in a Railscast. I removed the User.new guest creation command and the problem resolved.
I just re-create the whole RoR application, and copy all the controllers, models, and views that I built to the new app. It is now running well.
I tried to compare 2 versions and didn't have any result. Will let you guys know if I find out the cause of this.
Thanks all. :)
I had a similar problem. if you try to reload the schema (on local, dont do it on production obviously), you should see a difference in what you think you have and what the database actually thinks.
rake db:schema:dump
rake db:schema:load
THIS WILL ERASE YOUR DATA. But you should see a difference in the schema for 'users'. This was my difference:
The fresh dump showed:
create_table "posts", :id => :false, :force => true do |t|
t.integer "id", :null => false, :limit=> 8
t.integer "object_id", :limit => 8
t.string "post_type"
t.string "from_id"
t.text "picture_url"
t.text "video_source"
t.integer "shares"
t.integer "likes"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "image_size", :limit => 8
t.integer "vid_size", :limit => 8
end
But i know that id is handled by rails and this should be:
create_table "posts", :force => true do |t|
t.integer "object_id", :limit => 8
t.string "post_type"
t.string "from_id"
t.text "picture_url"
t.text "video_source"
t.integer "shares"
t.integer "likes"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "image_size", :limit => 8
t.integer "vid_size", :limit => 8
end
If this is also the case for you, simply add to your user model:
set_primary_key :id
That reset it and it works now. I still don't know the cause of the problem but this fixed it for me.
I am trying to run
heroku rake db:migrate
to run my migrations on heroku and the first two migrations ran great but the third which looks like this
create_table :charities, :options => "ENGINE=MyISAM" do |t|
t.string :name, :null => false
t.string :title, :null => false
t.timestamps
end
add_index :charities, :name
add_index :charities, :title
Migrating to CreateCharitiesAndThemes (20091019140537)
== CreateCharitiesAndThemes: migrating =======================================
-- create_table(:charities, {:options=>"ENGINE=MyISAM"})
rake aborted!
An error has occurred, this and all later migrations canceled:
PGError: ERROR: syntax error at or near "ENGINE"
LINE 1: ..., "created_at" timestamp, "updated_at" timestamp) ENGINE=MyI...
^
: CREATE TABLE "charities" ("id" serial primary key, "name" character varying(255) NOT NULL, "title" character varying(255) NOT NULL, "created_at" timestamp, "updated_at" timestamp) ENGINE=MyISAM
Heroku uses PostgreSQL, and MyISAM engine is MySQL-specific. I suggest you remove that part. Or, add checking on what database is used and make that optional.
Here's a link to how to check the database.
I'm trying to heroku rake db:reset from a Rails 3 app using sqlite3, but I'm getting the following error:
rake aborted!
PGError: ERROR: type modifier is not allowed for type "text"
LINE 1: ...ary key, "name" character varying(255), "content" text(255),...
^
here is my most recent migration:
change_table :mixes do |t|
t.change :content, :text
t.change :post, :text
end
and my schema.rb:
create_table "mixes", :force => true do |t|
t.string "name"
t.text "content", :limit => 255
t.datetime "created_at"
t.datetime "updated_at"
t.string "mixologist"
t.string "link"
t.string "title"
t.text "post", :limit => 255
end
From my understanding Sqlite3 doesn't enforce limits on string and text and I didn't add those limits myself. I thought Heroku would automatically handle those in converting to Postgres or whatever it does. But it seems like the limits are throwing it off somewhere. What's the best way for me to deal with this?
Let me know if I should post anything else.
Change your recent migration to
change_table :mixes do |t|
t.change :content, :text, :limit => nil
t.change :post, :text, :limit => nil
end
This is one of the many nuances you will have to look out for when developing using sqlite3 :( Only happens when you alter the type of a column from string to text.