【问题标题】:Rails 4 SQLiteExceptionRails 4 SQLite异常
【发布时间】:2016-01-06 13:29:28
【问题描述】:

我遇到了似乎导致问题的 SQLiteException。

Schema.rb

create_table "features", force: :cascade do |t|
t.string   "name_key"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "features", ["name_key"], name: "index_features_on_name_key"

create_table "organizations", force: :cascade do |t|
  t.string   "name"
  t.string   "code"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

create_table "organizations_features", id: false, force: :cascade do |t|
  t.integer "organization_id"
  t.integer "feature_id"
end

这是当前模式,我明确创建了表 organizations_features(仍通过迁移,但单独迁移引用了连接表),否则 create_join_table 将创建“features_organizations”。在那个过程中,如果我运行

rake db:drop db:create db:schema:load

即使没有在任何表中加载单个记录,我仍然会不断收到以下错误(我从未运行过 db:seed)。

ActiveRecord::StatementInvalid: SQLite3::SQLException: near ")": syntax error: INSERT INTO "organizations_features" () VALUES ()

other question 似乎建议将连接表名称全部设为单数,如 organization_feature 中一样,但由于我们与其他服务共享架构,因此我们必须使用相同的命名约定。

注意:即使我尝试使用迁移“create_join_table”创建表,问题似乎仍然存在”

更新:seeds.rb

organization = Organization.create!(name: 'XYZ', code: 'xyz')
feature = Feature.create!(name_key: 'Some Feature')
user = User.create!(name:  "user1",
                     email: "user@abcd.org",
                     password:              "password123",
                     password_confirmation: "password123",
                     profile_id: profile.id)
OrganizationsFeature.create!(feature_id: feature.id, organization_id:  organization.id)

OrganizationsFeature 看起来像这样

class OrganizationsFeature < ActiveRecord::Base
  belongs_to :organization
  belongs_to :feature
end

【问题讨论】:

  • 发布seed.rb文件的内容。
  • 用种子文件更新。
  • 所以,如果其他人也遇到问题,我找到了解决方案。

标签: ruby-on-rails sqlite


【解决方案1】:

我找到了问题的答案,以防其他人遇到同样的问题。 test/fixtures/organizations_features.yml 文件有空记录

one :{}  
two: {}

这导致空记录被插入到表中。每次都加载此数据,因此插入错误。 拥有正确的数据/删除文件解决了这个问题。

【讨论】:

    猜你喜欢
    • 2012-02-12
    • 1970-01-01
    • 2010-11-01
    • 2014-07-21
    • 2015-10-07
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    相关资源
    最近更新 更多