I have this migration file:
class InitialDigitizationWork < ActiveRecord::Migration
def self.up
create_table :digitizations do |t|
t.string :submission_code, :null => false
t.timestamps
end
add_index :digitizations, :submission_code, :unique => true
create_table :digitized_pieces do |t|
t.integer :digitization_id, :null => false
t.integer :position, :null => false
t.string :piece_type, :default => "Page"
t.timestamps
end
add_index :digitized_pieces, :digitization_id
create_table :digitized_views do |t|
t.integer :digitized_piece_id, :null => false
t.string :initial_file_name
t.string :attachment_file_name
t.string :attachment_content_type
t.integer :attachment_file_size
t.datetime :attachment_updated_at
t.integer :position, :null => false
t.boolean :is_primary, :default => false
t.timestamps
end
add_index :digitized_views, :digitized_piece_id, :null => false
end
and it fails on the last line:
-- add_index(:digitized_views, :digitized_piece_id, {:null=>false})
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Unknown key: :null. Valid keys are: :unique, :order, :name, :where, :length, :internal, :using, :algorithm,
Anyone have any idea what is going on?
What's annoying here is that since the last line fails, I can't rerun the migration again because the table on top digitizations exists. I know how to use Postgresql and access the specific table that is created specified by this yml file:
#SQLite version 3.x
#gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
adapter: mysql2
database: arthouse_development
username: root
password:
host: localhost
port: 3306
#socket: /tmp/mysql.sock
legacy_development:
adapter: mysql2
database: arthouse_legacy_development
username: root
password:
host: localhost
port: 3306
Anyone know how I can console into the database here?
Anyone know what might be going on? This is my first time using mysql and could use some help.
also when I type in mysql this happens:
mysql
ERROR 1045 (28000): Access denied for user 'jwan'#'localhost' (using password: NO)
but this works:
mysql -u root
Do I have to do that everytime?
Ok so here is what you need to do:
First rollback your last migration so that you begin on a clean slate:
rake db:rollback
Then remove the default=> null constraint on your indexes. The error message states explicitly that it's not a known key:
Unknown key: :null. Valid keys are: :unique, :order, :name, :where,
:length, :internal, :using, :algorithm,
Then rerun the migration:
rake db:migrate
Related
I'm running this migration:
class CreateAdmins < ActiveRecord::Migration[5.1]
def change
create_table :admins do |t|
t.string "first_name" :limit => 30
t.string "last name", :limit => 30
t.string "email", :default => '', :null => false
t.string "password" ,:limit => 40
t.timestamps
end
end
def down
drop_table :admins
end
end
I get an error saying:
rails db:migrate rails aborted! Mysql2::Error: Access denied for user
'root'#'localhost' (using password: YES)
For the first, this is correct migration code:
class CreateAdmins < ActiveRecord::Migration[5.1]
def self.up
create_table :admins do |t|
t.string :first_name, limit: 30
t.string :last_name, limit: 30
t.string :email, default: '', null: false
t.string :password ,limit: 40
t.timestamps
end
end
def self.down
drop_table :admins
end
end
or just change difference
class CreateAdmins < ActiveRecord::Migration[5.1]
def change
create_table :admins do |t|
t.string :first_name, limit: 30
t.string :last_name, limit: 30
t.string :email, default: '', null: false
t.string :password ,limit: 40
t.timestamps
end
end
end
About error, your database.yml for development env, should looks like this:
development:
adapter: mysql2
encoding: utf8
database: your_db_name
username: your_user
password: your_password
host: 127.0.0.1
port: 3306
You should update your database.yml file, in the config-directory. The error is caused by mysql responding with an authentication error, for your root user. You might wanna check http://edgeguides.rubyonrails.org/configuring.html#configuring-a-database for information on the database.yml syntax.
You can furthermore check and validate the login in shell by running mysql -u root -p and try entering your password.
i am a newbie trying to learn ruby on rails by following the Lynda video series using ROR version 5.0 and mysql 8.02. after creating my table entries in DB>migrate>model.rb file and running "rails db:migrate" i get an error in the terminal which does not make a lot of sense to. iv using a different user and grating it mysql privileges and searching on the web but no luck.
=============================
class CreateUsers < ActiveRecord::Migration[5.0]
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
MBP:simple_cms $ rails db:migrate
rails aborted!
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown collation: 'utf8_0900_ai_ci': CREATE TABLE schema_migrations (version varchar(255) COLLATE utf8_0900_ai_ci PRIMARY KEY) ENGINE=InnoDB
/Users/.rvm/gems/ruby-2.3.0/gems/mysql2-0.4.9/lib/mysql2/client.rb:120:in _query'
/Users/.rvm/gems/ruby-2.3.0/gems/mysql2-0.4.9/lib/mysql2/client.rb:120:inblock in query'
/Users/.rvm/gems/ruby-2.3.0/gems/mysql2-0.4.9/lib/mysql2/client.rb:119:in handle_interrupt'
/Users/.rvm/gems/ruby-2.3.0/gems/mysql2-0.4.9/lib/mysql2/client.rb:119:inquery'
/Users/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in block in execute'
/Users/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract_adapter.rb:590:inblock in log'
/Users/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.6/lib/active_support/notifications/instrumenter.rb:21:in instrument'
/Users/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract_adapter.rb:583:inlog'
/Users/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in execute'
/Users/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/mysql/database_statements.rb:31:inexecute'
/Users/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/schema_statements.rb:278:in create_table'
/Users/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:423:increate_table'
/Users/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.6/lib/active_record/schema_migration.rb:27:in create_table'
/Users/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.6/lib/active_record/connection_adapters/abstract/schema_statements.rb:1008:ininitialize_schema_migrations_table'
There was an issue related to your problem : https://github.com/rails/rails/issues/28730
This has been fixed in rails v5.1.0.rc2. Upgrading to the latest version and regenerating schema.rb should eliminate your problem.
Also it's possible to add general collation to your database.yml if utf8_0900_ai_ci doesn't work:
development:
adapter: mysql2
...
encoding: utf8
collation: utf8_general_ci
I am very new to Ruby and trying to complete a tutorial that I can't get to work properly. I am attempting to run rake db:migrate on my root folder and is giving me 3 separate error messages:
>rake db:migrate
rake aborted!
SyntaxError:
C:/Users/Bill/Sites/simple_cms/db/migrate/20170922050429_create_use
rs.rb:4: syntax error, unexpected keyword_do_block
create_table :users, do |t|
^
C:/Users/Bill/Sites/simple_cms/db/migrate/20170922050429_create_users.rb:5:
syntax error, unexpected tSTRING_BEG, expecting keyword_end
t.column "first_name", :string, :limit => 25
^
C:/Users/Bill/Sites/simple_cms/db/migrate/20170922050429_create_users.rb:5:
syntax error, unexpected ',', expecting keyword_end
t.column "first_name", :string, :limit => 25
^
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
The Ruby code from my create_users.rb file is:
class CreateUsers < ActiveRecord::Migration[5.1]
def change
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
I am not sure what I am doing wrong here. Any insight would be appreciated!
in addition to first answer. Why don't you use rails generators?
Write migrations can be usefull when you have to modify something. Faster and cleaner way is using rails generators:
Example:
rails g model User first_name:string last_name:string
If attributes are string you can just
rails g model User first_name last_name
It will generate class User in /app/models/user.rb and migration for database.
Also you have Scaffold, and others generators.
More info: Command Line Rails
Tip: Check for Devise Gem, it will generate entire structure for User Model.
Link: Devise Gem
In your code:
def change
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
You have:
create_table :users, do |t|
which should be:
create_table :users do |t|
There should not be any comma after that unless you have more than one arguments there.
Update:
And also you class has no end which will not close the class definition and throw exception.
On a side note:
You have this line:
t.column "first_name", :string, :limit => 25
which can be written as:
t.string "first_name", :limit => 25
Hope this helps.
in my clients table, I have a column named email. But when I made the tests for the clients controller and the model, the tests kept on saying that the clients table has no column named email.
SQLite3::SQLException: table clients has no column named email: CREATE UNIQUE INDEX "index_clients_on_email" ON "clients" ("email")
although I do admit that I didn't initially put that column when I created my table, but I added the column via a separate migration. I ran rake db:migrate and even tried rake db:drop:all, rake db:create:all and then rake db:migrate and it still didn't change anything.
the email column was also added as an index for the clients table.
this is my schema:
ActiveRecord::Schema.define(version: 20161230163248) do
create_table "clients", force: :cascade do |t|
t.string "name", null: false
t.text "email", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
add_index "clients", ["email"], name: "index_clients_on_email", unique: true
create_table "projects", force: :cascade do |t|
t.text "project_description", null: false
t.string "project_timescale"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "client_id"
end
add_index "projects", ["client_id"], name: "index_projects_on_client_id"
end
the initial migration for the clients table:
class CreateClients < ActiveRecord::Migration
def change
create_table :clients do |t|
t.string :name, presence: true, null: false
t.timestamps null: false
end
end
end
migration to add email as an index for the client table:
class AddIndexToClient < ActiveRecord::Migration
def change
add_index:clients, :email, unique: true
end
end
migration to add the email column:
class AddEmailToClient < ActiveRecord::Migration
def change
add_column :clients, :email, :text
end
end
the following is my database.yml:
# SQLite version 3.x
# gem install sqlite3
#
# Ensure the SQLite 3 gem is defined in your Gemfile
# gem 'sqlite3'
#
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
Try:
RAILS_ENV=test bundle exec rake db:schema:load
I have an MS SQL Server 2012 instance with managment studio and I have schema.rb file which contains the following:
# This file is auto-generated from the current state of the database.
# Note that this schema.rb definition is the authoritative source for your database schema.
ActiveRecord::Schema.define(:version => 20120525100324) do
create_table "academic_details", :force => true do |t|
t.integer "registration_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "additional_exam_groups", ["school_id"], :name => "index_additional_exam_groups_on_school_id", :limit => {"school_id"=>nil}
create_table "additional_exam_scores", :force => true do |t|
t.integer "student_id"
t.integer "additional_exam_id"
t.decimal "marks", :precision => 7, :scale => 2
t.integer "grading_level_id"
t.string "remarks"
t.boolean "is_failed"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "school_id"
end.................etc.
How to generate SQL Server database from that file?
First, is your Rails app properly configured to use MS SQL Server 2012 as its DB?
If so, try running rake db:schema:load task.
If that doesn't work, try running the rake db:setup task.
You can find more detailed information about the rake tasks here in this previous Stackoverflow answer: https://stackoverflow.com/a/10302357/631834