Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy.exc.OperationalError:(sqlite3.OperationalError)没有这样的表:user_Python_Sqlite_Flask_Sqlalchemy_Runtime Error - Fatal编程技术网

Python SQLAlchemy.exc.OperationalError:(sqlite3.OperationalError)没有这样的表:user

Python SQLAlchemy.exc.OperationalError:(sqlite3.OperationalError)没有这样的表:user,python,sqlite,flask,sqlalchemy,runtime-error,Python,Sqlite,Flask,Sqlalchemy,Runtime Error,run.py文件: from flaskblog import app, db if __name__ == '__main__': db.create_all() app.run(debug=True) models.py文件: from flaskblog import db from datetime import datetime class User(db.Model): __tablename__ = 'user' id = db.Colu

run.py文件:

from flaskblog import app, db

if __name__ == '__main__':
    db.create_all()
    app.run(debug=True)
models.py文件:

from flaskblog import db
from datetime import datetime



class User(db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)

    username = db.Column(db.String(20), nullable=False)

    email = db.Column(db.String(120), unique=True, nullable=False)

    img_file = db.Column(db.String(20), nullable=False, default='default.jpeg')

    password = db.Column(db.String(60), nullable=False)

    posts = db.relationship('Post', backref='author', lazy=True)


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



class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)

    title = db.Column(db.String(100), nullable=False)

    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

    content = db.Column(db.Text, nullable=False)

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)


    def __repr__(self):
        return f"User { self.title }, { self.date_posted })"
init.py文件:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_bcrypt import Bcrypt


app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
db = SQLAlchemy(app)
bcrypt = Bcrypt(app)


from flaskblog import routes
在使用
db.create_all()
运行应用程序之前,我曾尝试使用终端创建表,但仍然无法运行。 将引发以下错误:

SQLAlchemy.exc.OperationalError : (sqlite3.OperationalError) no such table: user
如果有人能解决这个问题,我将不胜感激。
谢谢。

#从flaskblog导入数据库创建表后,您是否可以使用
sqlite3
命令行客户端手动打开
site.db
文件并验证它是否确实存在?尝试将
db.create\u all()
放在模型架构之后。您必须使用与
SQLAlchemy
相同的实例,因此在您的模型定义中,您必须导入这些实例如果您使用
flask run
启动应用程序,则不会执行
if
位于
db.create()
底部的块。
#from flaskblog import db <- this import is wrong!

from my_project_name import db # the instancfe of SQLAlchemy that you use in your app
from datetime import datetime



class User(db.Model):
    __tablename__ = 'user'

    id = db.Column(db.Integer, primary_key=True)

    username = db.Column(db.String(20), nullable=False)

    email = db.Column(db.String(120), unique=True, nullable=False)

    img_file = db.Column(db.String(20), nullable=False, default='default.jpeg')

    password = db.Column(db.String(60), nullable=False)

    posts = db.relationship('Post', backref='author', lazy=True)


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



class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)

    title = db.Column(db.String(100), nullable=False)

    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)

    content = db.Column(db.Text, nullable=False)

    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)


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