【问题标题】:My sqlite database is not able to persist anything?我的 sqlite 数据库无法保存任何东西?
【发布时间】:2018-02-06 06:14:21
【问题描述】:

我有一个带有模型、控制器和序列化器的 Rails Api。数据库设置好了,我进行了几次迁移,所有这些都导致了架构的相应更改。但是,无论是在 Rails 控制台中还是从种子数据中,都没有将任何内容持久化到数据库中。例如,当我尝试在控制台中运行 User.create 时,我看到出现以下消息:

2.3.3 :003 > User.create
(0.1ms)  begin transaction
(0.1ms)  rollback transaction
=> #<User id: nil, email: "", created_at: nil, updated_at: nil>  

同样,我的种子文件中有这些数据:

users = User.create([{ email: 'adam@adam.com' }, { email: 'ryan@ryan.com' 
 }])
BankAccount.create(name: 'Adams Chase Checking Account', user_id: users.first) 

当我运行 rake db:seed 并尝试在 rails 控制台中调用 User.all 或 BankAccount.all 时,在这两种情况下我都会得到一个空数组。我听说过这样的错误是由模型上未满足的验证引起的,但我的模型没有任何验证。我不知道是什么导致了这个问题。任何帮助是极大的赞赏!另外,值得一提的是,这个项目使用 Rails 5.1.4,而我之前只使用过 4.x.x。这是用户模型(使用设计):

class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
     :recoverable, :rememberable, :trackable, :validatable 
has_many :bank_accounts 
has_many :credit_cards
has_many :investments 
has_many :loans 
has_many :assets 
has_many :recurring_payments, through: :bank_accounts
has_many :recurring_payments, through: :credit_cards
has_many :recurring_payments, through: :investments
has_many :recurring_payments, through: :loans
end  

这里是银行账户模型:

class BankAccount < ApplicationRecord 
belongs_to :user 
has_many :recurring_payments 
accepts_nested_attributes_for :recurring_payments
end

这是完整的架构:

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

create_table "assets", force: :cascade do |t|
t.string "name"
t.integer "value"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "bank_accounts", force: :cascade do |t|
t.string "name"
t.integer "balance"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "credit_cards", force: :cascade do |t|
t.string "provider"
t.integer "balance"
t.integer "interest_rate"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "investments", force: :cascade do |t|
t.string "name"
t.integer "value"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "loans", force: :cascade do |t|
t.integer "user_id"
t.integer "interest_rate"
t.integer "remaining_balance"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "recurring_payments", force: :cascade do |t|
t.string "source"
t.boolean "status"
t.date "pay_date"
t.integer "pay_amount"
t.integer "duration"
t.integer "bank_account_id"
t.integer "credit_card_id"
t.integer "loan_id"
t.integer "investment_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: 
"index_users_on_reset_password_token", unique: true
end

end

【问题讨论】:

    标签: ruby-on-rails sqlite ruby-on-rails-5 rails-api


    【解决方案1】:

    我的猜测是这样的。

    users = User.create([{ email: 'adam@adam.com' }, { email: 'ryan@ryan.com' }])
    

    Devise 在保存前验证默认密码(最少 6 个字符)。试试在控制台运行这个命令,看看有没有报错?

    user = User.create(email: 'adam@adam.com')
    user.errors
    

    【讨论】:

    • 啊,你说得对,它说密码不能为空。但是,这并不能解释创建银行账户或任何其他模型的错误(我不认为)?
    • BankAccount 引用了用户,我猜这可能是问题所在。
    • 太棒了。不要忘记投票或将其标记为答案。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-29
    • 2012-06-19
    相关资源
    最近更新 更多