我的 sqlite 数据库无法保存任何内容?
My sqlite database is not able to persist anything?
我有一个 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
我猜是这样的。
users = User.create([{ email: 'adam@adam.com' }, { email: 'ryan@ryan.com' }])
Devise 在保存前验证默认密码(最少 6 个字符)。在控制台中尝试 运行 这个命令,看看它是否抛出任何错误?
user = User.create(email: 'adam@adam.com')
user.errors
我有一个 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
我猜是这样的。
users = User.create([{ email: 'adam@adam.com' }, { email: 'ryan@ryan.com' }])
Devise 在保存前验证默认密码(最少 6 个字符)。在控制台中尝试 运行 这个命令,看看它是否抛出任何错误?
user = User.create(email: 'adam@adam.com')
user.errors