Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/343.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.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 3.4-数据库模型错误_Python_Python 3.x_Flask_Flask Sqlalchemy - Fatal编程技术网

烧瓶及;Python 3.4-数据库模型错误

烧瓶及;Python 3.4-数据库模型错误,python,python-3.x,flask,flask-sqlalchemy,Python,Python 3.x,Flask,Flask Sqlalchemy,我已经和我的应用程序的模型搏斗了一段时间了。每次我似乎“修复”了一件事,另一件事就崩溃了。通过谷歌搜索,我终于发现了这个问题,然而,它现在又抛出了另一个错误,我不知道为什么 from flask import Flask, render_template from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students

我已经和我的应用程序的模型搏斗了一段时间了。每次我似乎“修复”了一件事,另一件事就崩溃了。通过谷歌搜索,我终于发现了这个问题,然而,它现在又抛出了另一个错误,我不知道为什么

from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///students.db'
db = SQLAlchemy(app)


class Students(db.Model):
    __tablename__ = 'Students'
    id = db.Column(db.Integer, primary_key=True, unique=True)
    first_name = db.Column(db.String(20), primary_key=False, unique=True)
    last_name = db.Column(db.String(20), primary_key=False, unique=True)
    image = db.Column(db.String(20), primary_key=False, unique=True)

    def __init__(self, id, first_name, last_name, image):
        self.id = id
        self.first_name = first_name
        self.last_name = last_name
        self.image = image

    def __repr__(self):
        return '<Student: {} {}>'.format(self.first_name, self.last_name)

@app.route('/')
def index():
    s = Students.query.all()
    return render_template('index.html', Students=s)


if __name__ == '__main__':
    app.run(debug=True)

现在我在这里的某个地方读到,因为我是从模型继承的,所以我不必包括
\uuuu tablename\uuuu
,但如果没有它,我会出现以下错误:

sqlalchemy.exc.InvalidRequestError: Class <class '__main__.Students'> does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class.

我做错了什么?

我不太懂sqlalchemy,但我怀疑你不应该重写
\uuuu init\uuuu
方法。pip freeze给你带来了什么?我试图用Flask==0.10.1、Flask SQLAlchemy==1.0和SQLAlchemy==0.9.4复制您的问题,但我无法这样做(我还可以从其他列声明中删除
主键
唯一的
关键字args。@SeanVieira运行
pip freeze
给了我以下信息:
Flask==0.10.1 Flask SQLAlchemy==1.0 Jinja2==2.7.2 MarkupSafe==0.23 SQLAlchemy==0.9.4 Werkzeug==0.9)ous==0.24
您可能希望删除
.pycache
目录和您周围的所有
*.pyc
文件-从目前的情况看,您和我似乎使用相同的代码获得完全不同的结果(虽然理论上可能,但这是极不可能的)。
Column() missing 1 required positional argument: 'primary_key'
sqlalchemy.exc.InvalidRequestError: Class <class '__main__.Students'> does not have a __table__ or __tablename__ specified and does not inherit from an existing table-mapped class.
sqlalchemy.exc.ArgumentError: Mapper Mapper|Students|Students could not assemble any primary key columns for mapped table 'Students'