Rake 不创建表
Rake Not Creating Tables
问题
rake db:migrate
没有在我的 MySQL 数据库中创建 table。 (是的,我已经阅读了所有类似的帖子,并实施了他们的建议,请继续阅读。)
代码
database.yml:
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
host: localhost
port: /tmp/mysql.sock
development:
<<: *default
database: asreport
来自 gem 文件的行(是的,我也已经 gem 安装了它):
gem 'mysql2', '~> 0.3.20'
/appfile/db/migrate/create_users.rb(我也尝试制作第二行 'def up'):
class CreateUsers < ActiveRecord::Migration
def up
create_table :users do |t|
t.string :username
t.string :password
t.integer :usertype
t.string :salt
t.timestamps
end
end
end
我运行rake db:drop
,rake db:create
刷新后,rakedb:migrate--trace读取(此输出后,'show tables'inmysql 仍然只显示 schema_migrations):
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:dump
我试过的
首先,我知道我正在通过 Ruby 连接到 MySQL,因为 db:drop
create 确实创建了数据库本身,而不是 table .
我已经阅读了我能找到的有关该问题的所有相关堆栈溢出帖子。我试过回滚,将我的数据库直接放在 SQL 和 db:drop/create
.
上
我也尝试删除并重新创建我的迁移脚本。
我 运行 db:migrate 多次(单独和 db:drop/create's, rollback's, resets)
之后,但是 schema_migrations 总是有 0 个条目,而我的 schema.rb
文件在 version: 0
.
您没有遵循 Rails Guides 中概述的命名约定。
具体来说,这个:
2.1
Creating a Standalone Migration Migrations are stored as files in the db/migrate directory, one for each migration class. The name of
the file is of the form YYYYMMDDHHMMSS_create_products.rb, that is to
say a UTC timestamp identifying the migration followed by an
underscore followed by the name of the migration. The name of the
migration class (CamelCased version) should match the latter part of
the file name. For example 20080906120000_create_products.rb should
define class CreateProducts and
20080906120001_add_details_to_products.rb should define
AddDetailsToProducts. Rails uses this timestamp to determine which
migration should be run and in what order, so if you're copying a
migration from another application or generate a file yourself, be
aware of its position in the order.
继续尝试虚拟迁移以查看文件名约定。
$ bin/rails generate migration AddPartNumberToProducts part_number:string
问题
rake db:migrate
没有在我的 MySQL 数据库中创建 table。 (是的,我已经阅读了所有类似的帖子,并实施了他们的建议,请继续阅读。)
代码
database.yml:
default: &default
adapter: mysql2
encoding: utf8
pool: 5
username: root
password:
host: localhost
port: /tmp/mysql.sock
development:
<<: *default
database: asreport
来自 gem 文件的行(是的,我也已经 gem 安装了它):
gem 'mysql2', '~> 0.3.20'
/appfile/db/migrate/create_users.rb(我也尝试制作第二行 'def up'):
class CreateUsers < ActiveRecord::Migration
def up
create_table :users do |t|
t.string :username
t.string :password
t.integer :usertype
t.string :salt
t.timestamps
end
end
end
我运行rake db:drop
,rake db:create
刷新后,rakedb:migrate--trace读取(此输出后,'show tables'inmysql 仍然只显示 schema_migrations):
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:schema:dump
我试过的
首先,我知道我正在通过 Ruby 连接到 MySQL,因为 db:drop
create 确实创建了数据库本身,而不是 table .
我已经阅读了我能找到的有关该问题的所有相关堆栈溢出帖子。我试过回滚,将我的数据库直接放在 SQL 和 db:drop/create
.
我也尝试删除并重新创建我的迁移脚本。
我 运行 db:migrate 多次(单独和 db:drop/create's, rollback's, resets)
之后,但是 schema_migrations 总是有 0 个条目,而我的 schema.rb
文件在 version: 0
.
您没有遵循 Rails Guides 中概述的命名约定。
具体来说,这个:
2.1 Creating a Standalone Migration Migrations are stored as files in the db/migrate directory, one for each migration class. The name of the file is of the form YYYYMMDDHHMMSS_create_products.rb, that is to say a UTC timestamp identifying the migration followed by an underscore followed by the name of the migration. The name of the migration class (CamelCased version) should match the latter part of the file name. For example 20080906120000_create_products.rb should define class CreateProducts and 20080906120001_add_details_to_products.rb should define AddDetailsToProducts. Rails uses this timestamp to determine which migration should be run and in what order, so if you're copying a migration from another application or generate a file yourself, be aware of its position in the order.
继续尝试虚拟迁移以查看文件名约定。
$ bin/rails generate migration AddPartNumberToProducts part_number:string