【问题标题】:Unable to see tables after rake db:migrate in sqlite3rake db:migrate 在 sqlite3 后无法查看表
【发布时间】:2014-02-11 09:48:23
【问题描述】:

我是 Rails 新手。我正在使用 Rails 4.0.0 和 ruby​​ 2.0.0。我正在通过 Kevin Skoglund 的 Ruby on Rails 4 基本培训学习。他用的是mysql,但我无法安装,所以我切换到sqlite3,一切正常。

问题:我正在运行 rake db:migrate。而且它似乎运行正常,但是我在sqlite3中看不到db中的表。

详细信息:使用

创建了数据库 simple_cms_development
sqlite3 simple_cms_development.db

通过将 simple_cms_development 的位置设置为 'development:' 中的 'database:' 来配置 database.yml 中的项目。然后跑了

rake db:schema:dump 将 Rails 连接到数据库并导出模式。

然后我使用模型生成迁移

rails 生成模型用户

在 create_users.db 中

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

然后我运行了迁移rake db:migrate。我得到了:

==  DoNothingYet: migrating ================
==  DoNothingYet: migrated (0.0000s) =======

==  CreateUsers: migrating =================
-- create_table(:users)
   -> 0.1941s
==  CreateUsers: migrated (0.1951s) ========

我检查了 schema.rb,它有:

ActiveRecord::Schema.define(version: 20140121101037) do

  create_table "users", force: true do |t|
    t.string   "first_name", limit: 25
    t.string   "last_name",  limit: 50
    t.string   "email",                 default: "", null: false
    t.string   "password",   limit: 40
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

到目前为止一切正常。

然后我在 sqlite3 中打开 simple_cms_development.db,并尝试查看 .tables 的表,它什么也没显示,没有错误。它只显示另一个 sqlite 提示。它应该显示 schema_migrations 和用户。我该怎么办?

【问题讨论】:

  • Rails 将使用/config/database.yml 文件中指定的任何数据库——而不是您从命令行创建的数据库。对于 SQLite 数据库,这些通常存储在/db 目录下。你能检查一下你的 database.yml 文件里有什么吗?
  • 有一个类似的问题,即作为添加列迁移目标的表不再可供 Rails 测试访问。

标签: ruby-on-rails sqlite rake


【解决方案1】:

从终端转到您的项目目录并粘贴以下行:

sqlite3 -line db/development.sqlite3

然后您将找到数据库控制台,现在您可以在此处编写查询,如下所示:

sqlite> 从用户中选择 *;

【讨论】:

    【解决方案2】:

    阅读this post,正在运行

    rails db:drop db:create db:migrate RAILS_ENV=test
    

    为我修好了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 2011-10-31
      • 1970-01-01
      • 2017-07-30
      • 2015-07-21
      • 2015-06-10
      相关资源
      最近更新 更多