I am getting this error when I run db:migrate
Mysql2::Error: Table 'sample_app_development.microposts' doesn't exist: SHOW KEYS FROM
`microposts
This is my migration
class CreateMicroposts < ActiveRecord::Migration
def change
create_table :microposts do |t|
t.string :content
t.integer :user_id
t.timestamps
add_index :microposts, [:user_id, :created_at]
end
end
end
I have tried restarting mysql and deleting and recreating the database.
Move your add_index out of the create_table's block.
Related
I am trying to make it so certain columns cannot be null. I use change_column no problem but some of these columns have an index attached to them signifying that it is unique.
However when I run this migration:
change_column :users, :username, :string, null: false
change_column :users, :email, :string, null: false
change_column :users, :password, :string, null: false
change_column :users, :terms_agreed, :boolean, null: false
It removes the add_index in the schema
schema.rb
- add_index "users", ["username"], :name => "index_users_on_username_code", :unique => true
- add_index "users", ["email"], :name => "index_users_on_email_code", :unique => true
add_index "users", ["confirmation_code"], :name => "index_users_on_confirmation_code", :unique => true
How do I do this without removing the indexes?
P.S. its not actually removing the indexes in the database. Just in the schema.rb file.
The behaviour of how migrations are treated depends on you database implementation. More info here, but in your migration you should explicitly request an index in your change.
class DoSomethingToTable < ActiveRecord::Migration
def change
change_column :users, :username, :string, null: false, index: true
end
end
See the docs for more information.
So still unclear as to what happened, but turns out that the indexes got deleted from the database during some database manipulation.
I just put the schema back the way I needed it to be and ran rake db:setup then my migration again and everything worked fine. Very confusing and a lot of unnecessary time spent trying to figure it out.
I'm just following this RoR tut, I'm doing it the same way but I'm stuck creating a table:
$ rails generate model User
invoke active_record
create db/migrate/20140718180319_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
This is my xxxxx_create_users.rb
class CreateUsers < ActiveRecord::Migration
def Up
create_table :users do |t|
t.column "first_name", :string, :limit => 25
t.string "last_name", :limit => 50
t.string "email", :default => "", :null => false
t.string "password", :limit => 40
t.timestamps
end
end
def down
drop_table :users
end
end
When I run db:migrate the table is not being created:
$ rake db:migrate
== 20140718182504 CreateUsers: migrating ======================================
== 20140718182504 CreateUsers: migrated (0.0000s) =============================
Is missing
create_table(:users)
-> x.xxxxxs
What am I doing wrong? Thanks.
Isn't it a typo with your "Up" migration method? Try with:
def up
instead of:
def Up
I am integrating a CAS client using rack-cas gem on an application using mysql database, When i run,
rails generate cas_session_store_migration as specified on the readme, it generates a migration 20140428130031_create_rack_cas_sessions.rb which looks like this:
class CreateRackCasSessions < ActiveRecord::Migration
def self.up
create_table :sessions do |t|
t.string :session_id, :null => false
t.string :cas_ticket
t.text :data
t.timestamps
end
add_index :sessions, :session_id
add_index :sessions, :cas_ticket
add_index :sessions, :updated_at
end
def self.down
drop_table :sessions
end
end
when i run rake db:migrate or bundle exec rake db:migrate nothing happens. The migration fails to take place and there is no error message(s). The same migration on another service using sqlite database migrates successfully.
I want to add another index column to my index table,but receive an index on table is too long; the limit is 64 characters. Rails 3 and MySQL DB by the way.
The current schema is as follows:
create_table "admin_users_projects", :force => true do |t|
t.integer "admin_user_id"
t.integer "project_id"
end
add_index "admin_users_projects", ["admin_user_id", "project_id"], :name => "index_admin_users_projects_on_admin_user_id_and_project_id"
I am trying to run the following migration to add the index:
class AddIndexToAdminUsersProjects < ActiveRecord::Migration
def change
add_index :index_admin_users_projects_on_admin_user_id_and_project_id, :admin_users_project_id
end
end
But get the following when attempting the rake (in short):
Larrys-MacBook-Pro:scrumtool larrydavid$ rake db:migrate
== AddIndexToAdminUsersProjects: migrating ===================================
-- add_index(:index_admin_users_projects_on_admin_user_id_and_project_id, :admin_users_project_id)
rake aborted!
An error has occurred, all later migrations canceled:
Index name 'index_index_admin_users_projects_on_admin_user_id_and_project_id_on_admin_users_project_id' on table 'index_admin_users_projects_on_admin_user_id_and_project_id' is too long; the limit is 64 characters
[...]
Try this
class AddIndexToAdminUsersProjects < ActiveRecord::Migration
def change
add_index :admin_users_project, [:admin_users_id, :project_id], :name => 'index_admin_projects_on_admin_and_project'
end
end
I created a Heroku app which utilizes the gmaps4fails gem for one of the application's features.
When I tried to seed the database, I get
rake aborted!
undefined local variable or method 'acts_as_gmappable' for #<Class:0x00000004b5b928>
Any ideas how to fix this?
Everything works fine when testing locally.
Edit: Here's my model code
class Hall < ActiveRecord::Base
has_many :hall_features
has_many :green_features, :through => :hall_features
has_many :settings, :through => :pinned_halls
belongs_to :operational_unit
acts_as_gmappable
...
Migration:
class CreateHalls < ActiveRecord::Migration
def up
create_table 'halls' do |t|
t.string 'name'
t.text 'background'
t.text 'energy_info'
t.float 'latitude'
t.float 'longitude'
t.boolean 'gmaps'
t.timestamps
end
end
def down
drop_table 'halls' # deletes the whole table and all its data!
end
end
Here's the error from my terminal: http://i48.tinypic.com/zvoggg.png
Ugh .. put the gem under :development in Gemfile only. Including it in :production solved everything.
I'll be crying myself to sleep tonight. Some tears of happiness too.