mysql - 创建执行 rake db :migrate in Rails 2. 0.2 和 MySql 5.1 的表时出错

标签 mysql ruby-on-rails version-compatibility

我正在尝试从网站 http://oreilly.com/ruby/archive/rails-revisited.html 实现一个 Rails Recipe 应用程序.他们给出了一个很好的例子,强调了 Rails 对于敏捷开发的重要性。这篇文章中的示例是在低于 1.x 的 Rails 版本中实现的。

我正在尝试在 2.0.2 版中实现相同的功能。他们似乎已经使用 MySql 4.1 数据库实现了它。我目前在 Ubuntu 10.04 平台上使用 mysql-client 5.1。当我尝试使用 rake db:migrate 时,我遇到了某些错误。

即使在手动创建数据库表时,我最初也遇到了错误。似乎某些属性的创建数据库表的语法已更改。例如 帖子中创建食谱表的命令对我不起作用

create table recipes (
    id                     int            not null auto_increment,
    category_id            int            not null,
    title                  varchar(100)   not null default '',
    description            varchar(255)   null,
    date                   date           null,
    instructions           text           null,
    constraint fk_recipes_categories foreign key (category_id) references categories(id),
    primary key(id)
) engine=InnoDB;

直到我更改了一些语法以适应 5.1 版。对我有用的命令是:

create table recipes ( id int not null auto_increment, category_id int not null, title varchar(100) not null default '', description varchar(255) null, date date null, instructions text null, primary key (id), constraint fk_recipes_categories foreign key (category_id) references categories(id)) engine=InnoDB;

现在回到我的问题。我实际上是在尝试进行某种逆向工程,只是为了了解脚手架的工作原理。最初我想了解它的重要性,所以我尝试手动操作..

然后我尝试使用 scaffold 命令做事,我目前被 rake db:migrate 困住了。我知道它可能看起来有点太多引用一个太大的问题,但我想你需要检查一下你可能会告诉我我需要合并哪些更改才能使用 rake db:migrate

创建我的表
mohnish@pc146724-desktop:~/railsprg/2011/Jan11/12jan11_recipes/cookbook2$ script/generate scaffold category id:integer name:string
/usr/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/rails_generator/lookup.rb:207:Warning: Gem::SourceIndex#search support for Regexp patterns is deprecated, use #find_name
      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/categories
      exists  app/views/layouts/
      exists  test/functional/
      exists  test/unit/
      create  app/views/categories/index.html.erb
      create  app/views/categories/show.html.erb
      create  app/views/categories/new.html.erb
      create  app/views/categories/edit.html.erb
      create  app/views/layouts/categories.html.erb
      create  public/stylesheets/scaffold.css
  dependency  model
/usr/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/rails_generator/lookup.rb:207:Warning: Gem::SourceIndex#search support for Regexp patterns is deprecated, use #find_name
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
      create    app/models/category.rb
      create    test/unit/category_test.rb
      create    test/fixtures/categories.yml
      create    db/migrate
      create    db/migrate/001_create_categories.rb
      create  app/controllers/categories_controller.rb
      create  test/functional/categories_controller_test.rb
      create  app/helpers/categories_helper.rb
       route  map.resources :categories
mohnish@pc146724-desktop:~/railsprg/2011/Jan11/12jan11_recipes/cookbook2$ script/generate scaffold recipe id:integer category_id:integer title:string description:text date:date instructions:text
/usr/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/rails_generator/lookup.rb:207:Warning: Gem::SourceIndex#search support for Regexp patterns is deprecated, use #find_name
      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/recipes
      exists  app/views/layouts/
      exists  test/functional/
      exists  test/unit/
      create  app/views/recipes/index.html.erb
      create  app/views/recipes/show.html.erb
      create  app/views/recipes/new.html.erb
      create  app/views/recipes/edit.html.erb
      create  app/views/layouts/recipes.html.erb
   identical  public/stylesheets/scaffold.css
  dependency  model
/usr/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/rails_generator/lookup.rb:207:Warning: Gem::SourceIndex#search support for Regexp patterns is deprecated, use #find_name
      exists    app/models/
      exists    test/unit/
      exists    test/fixtures/
      create    app/models/recipe.rb
      create    test/unit/recipe_test.rb
      create    test/fixtures/recipes.yml
      exists    db/migrate
      create    db/migrate/002_create_recipes.rb
      create  app/controllers/recipes_controller.rb
      create  test/functional/recipes_controller_test.rb
      create  app/helpers/recipes_helper.rb
       route  map.resources :recipes
mohnish@pc146724-desktop:~/railsprg/2011/Jan11/12jan11_recipes/cookbook2$ rake db:create
(in /home/mohnish/railsprg/2011/Jan11/12jan11_recipes/cookbook2)
"db/development.sqlite3 already exists"
mohnish@pc146724-desktop:~/railsprg/2011/Jan11/12jan11_recipes/cookbook2$ rake db:create
(in /home/mohnish/railsprg/2011/Jan11/12jan11_recipes/cookbook2)
mohnish@pc146724-desktop:~/railsprg/2011/Jan11/12jan11_recipes/cookbook2$ rake db:migrate
(in /home/mohnish/railsprg/2011/Jan11/12jan11_recipes/cookbook2)
== 1 CreateCategories: migrating ==============================================
-- create_table(:categories)
rake aborted!
Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(11), `name` varchar(255) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `upd' at line 1: CREATE TABLE `categories` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY(11), `name` varchar(255) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL) ENGINE=InnoDB

(See full trace by running task with --trace)
mohnish@pc146724-desktop:~/railsprg/2011/Jan11/12jan11_recipes/cookbook2$ rake db:migrate --trace
(in /home/mohnish/railsprg/2011/Jan11/12jan11_recipes/cookbook2)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== 1 CreateCategories: migrating ==============================================
-- create_table(:categories)
rake aborted!
Mysql::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(11), `name` varchar(255) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `upd' at line 1: CREATE TABLE `categories` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY(11), `name` varchar(255) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL) ENGINE=InnoDB
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:150:in `log'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/mysql_adapter.rb:281:in `execute'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:104:in `create_table'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/connection_adapters/mysql_adapter.rb:416:in `create_table'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:285:in `send'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:285:in `method_missing'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:265:in `say_with_time'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:265:in `say_with_time'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:281:in `method_missing'
./db/migrate//001_create_categories.rb:3:in `up_without_benchmarks'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:219:in `send'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:219:in `migrate'
/usr/lib/ruby/1.8/benchmark.rb:293:in `measure'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:219:in `migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:348:in `migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:339:in `each'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:339:in `migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:307:in `up'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.0.2/lib/active_record/migration.rb:298:in `migrate'
/usr/lib/ruby/gems/1.8/gems/rails-2.0.2/lib/tasks/databases.rake:85
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `call'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:636:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:631:in `execute'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:597:in `invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:in `synchronize'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:590:in `invoke_with_call_chain'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:583:in `invoke'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2051:in `invoke_task'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `each'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2029:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2023:in `top_level'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2001:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:2068:in `standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake.rb:1998:in `run'
/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/bin/rake:31
/usr/bin/rake:19:in `load'
/usr/bin/rake:19
mohnish@pc146724-desktop:~/railsprg/2011/Jan11/12jan11_recipes/cookbook2$

db/migrate/001_create_categories.rb 和 db/migrate/002_create_recipes.rb 看起来像这样:

class CreateCategories < ActiveRecord::Migration
  def self.up
    create_table :categories do |t|
      t.integer :id
      t.string :name

      t.timestamps
    end
  end

  def self.down
    drop_table :categories
  end
end

class CreateRecipes < ActiveRecord::Migration
  def self.up
    create_table :recipes do |t|
      t.integer :id
      t.integer :category_id
      t.string :title
      t.text :description
      t.date :date
      t.text :instructions

      t.timestamps
    end
  end

  def self.down
    drop_table :recipes
  end
end

谢谢你的帮助..

最佳答案

您的迁移不需要t.integer :id,您可以免费获得它。将其删除。

http://guides.rubyonrails.org/v2.3.8/migrations.html是一个很好的资源。

关于mysql - 创建执行 rake db :migrate in Rails 2. 0.2 和 MySql 5.1 的表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4676839/

相关文章:

php - 如何使用Mysql查询获得以下结果集

mysql - Codeception DB模块异常

ruby-on-rails - 从 url 导入 CSV Errno::ENAMETOOLONG: 文件名太长

ruby-on-rails - ActiveAdmin Globalize 创建索引过滤器

node.js - 卸载使用不同版本的 npm 安装的 bower

java - 将应用程序引擎 JDO 应用程序移植到 MySql 后端

mysql - 使用 Ruby/Rails 基于信息架构生成 MySQL View

javascript - 如何将 Rails 数据传递给 JS 或 Coffeescript?

xamarin.ios - Monotouch : 5. CheckVersion 中的 1.0 Beta API 更改