replace_gem:在创建模型时加载“ sqlite3” Active Record适配器时出错

时间:2019-02-05 07:45:04

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

在创建的项目中创建模型时出现错误,错误如下,

  

/home/sushmitha/.rvm/gems/ruby-2.5.1/gems/bundler-2.0.1/lib/bundler/rubygems_integration.rb:408:在replace_gem中的块(2个级别)中:错误加载“ sqlite3” Active Record适配器。缺少依赖的宝石吗?无法激活sqlite3(〜> 1.3.6),已激活sqlite3-1.4.0。确保所有依赖项都已添加到Gemfile中。 (LoadError)

4 个答案:

答案 0 :(得分:15)

对于rails 5.2.2,明确地将您的Gemfile更新为sqlite:

from datetime import datetime
from flask import Flask, render_template, url_for,flash,redirect
from flask_sqlalchemy import SQLAlchemy
from forms import RegistrationForm, LoginForm

app = Flask(__name__)    #name of the module
app.config['SECRET_KEY']= 'b2ffa54db1c495dab1f21973b39c400a'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
db =  SQLAlchemy(app)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(25), unique=True, 
nullable=False)
    email = db.Column(db.String(100), unique=True, nullable=False)
    password = db.Column(db.String(100), unique=True, 
nullable=False)
    image_file = db.Column(db.String(20), nullable=False, 
default='default.jpg')
    posts = db.relationship('Post', backreff='author', lazy=True)


    def __repr__(self):
       return f"user('{self.username}','{self.email}', '{self.image_file}')"

class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(120), nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    content = db.Column(db.Text, nullable=False)

def __repr__(self):
       return f"post('{self.title}','{self.date_posted}')"

并在终端使用:

gem 'sqlite3', '~> 1.3.6'

答案 1 :(得分:14)

仅供参考,此处添加以下内容,

对于Rails 5+,通过使用sqlite数据库在Gemfile中为SQLite宝石指定适当的版本来解决此问题:

 gem 'sqlite3', '~> 1.3', '>= 1.3.6'

Reference

答案 2 :(得分:1)

我已将gem 'sqlite3', '~> 1.3.6'放在group :deevelopment, :test do里面,它对我有用。

例如:

group :development, :test do
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'sqlite3', '~> 1.3.6'
end

答案 3 :(得分:0)

现在,rails 5.2.2.1 has been released的正确且也是最简单的解决方法是将rails扩展到该版本,因为它将sqlite3限制为1.3.6

# In Gemfile
gem 'rails', '~> 5.2.2.1'

然后在终端机

bundle install

鉴于该版本中的安全修复程序,无论如何都应这样做。