【问题标题】:how to add table to sqlite3 database如何将表添加到 sqlite3 数据库
【发布时间】:2014-03-19 08:48:58
【问题描述】:

如何将数据库添加到我的 SQLite3 数据库? 我总是收到这个错误。

ActiveRecord::StatementInvalid in Timespans#index

Showing C:/xampp/htdocs/fluxcapacitor/app/views/timespans/index.html.erb where line #19 raised:

SQLite3::SQLException: no such table: timespans: SELECT "timespans".* FROM "timespans"

我尝试将我的表添加到 myschema.rb,但它不起作用。当我运行时,它总是被覆盖

rake db:migrate RAILS_ENV=development

【问题讨论】:

    标签: sql ruby-on-rails ruby sqlite


    【解决方案1】:

    你应该生成migration:

    bundle exec rails g migration create_timespans
    

    在迁移过程中,您应该:

    class CreateTimespans < ActiveRecord::Migration
      def change
        create_table :timespans
          t.string :column1
          t.string :column2
          # ...
        end
      end
    end
    

    然后使用bundle exec rake db:migrate 运行它。

    这是在 Rails 应用程序中更改数据库架构的正确方法。

    【讨论】:

    • 谢谢。为我工作!
    【解决方案2】:

    您想使用迁移来更改您的数据库。详情请见the Active Record Migrations guide

    一般来说,流程是这样的:

    1. rails generate migration create_timespans 为您生成一个空迁移。
    2. 编辑创建的迁移以按照您的意愿设置表。这应该与您在 schema.rb 中添加的内容非常相似。
    3. rake db:migrate 然后将这些更改应用到数据库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-03
      • 2021-11-27
      • 1970-01-01
      • 2016-10-30
      • 1970-01-01
      • 2021-04-28
      • 2019-09-18
      • 1970-01-01
      相关资源
      最近更新 更多