【问题标题】:ruby (5) on rails - SQLite3::SQLException: no such tableruby (5) on rails - SQLite3::SQLException: no such table
【发布时间】:2017-12-07 00:17:53
【问题描述】:

我在我的 SQLite 数据库中创建了另一个表,但遗憾的是无法通过 rails 控制台访问它。我通过 GUI(SQLite 的 DB 浏览器)添加了一行数据,现在尝试通过它访问它,但它给了我错误 "ActiveRecord::StatementInvalid: SQLite3::SQLException: 没有这样的表:"

在我的架构中我已经可以看到它了:

create_table "user_information", force: :cascade do |t|
t.string "charity_name", limit: 100
t.string "res_exp_contact_name", limit: 50
t.string "res_exp_contact_email", limit: 100, default: "", null: false
t.string "grants_contact_name", limit: 50
t.string "grants_contact_email", limit: 100, default: "", null: false
t.boolean "submission_status_res_exp"
t.integer "submission_status_grants"
t.string "category"
t.string "username"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.index ["username"], name: "index_user_information_on_username"
end

这是终端的样子:

ck$ rails console
Running via Spring preloader in process 8442
Loading development environment (Rails 5.1.4)
Cannot read termcap database;
using dumb terminal settings.
irb(main):001:0> user = UserInformation.first
   UserInformation Load (0.2ms)  SELECT  "user_informations".* FROM 
   "user_informations" ORDER BY "user_informations"."id" ASC LIMIT ?  
[["LIMIT", 1]]
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: 
user_informations: SELECT  "user_informations".* FROM 
"user_informations" ORDER BY "user_informations"."id" ASC LIMIT ?
    from (irb):1

有人知道这个问题的解决方案吗?我很感激任何提示! :-)

【问题讨论】:

  • create_table "user_information" vs SELECT "user_informations" => create_table 使用单数形式,但 Rails(默认情况下)将表的名称计算为复数形式。您可以在UserInformation 模型中手动设置表的名称(不推荐)或回滚然后将迁移中的表重命名为create_table "user_informations"(!!!这将删除此表中的所有现有数据,创建一个新迁移如果要保留现有数据,则重命名表)

标签: ruby-on-rails sqlite sqlexception


【解决方案1】:

解决了。答案是您总是以复数形式命名表,因此我添加了一个迁移,将我的表从“user_information”重命名为“user_informations”。现在可以了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 2019-09-17
    • 2021-01-31
    • 2023-03-31
    • 2022-06-15
    • 1970-01-01
    • 2012-10-07
    相关资源
    最近更新 更多