錯誤:sqlalchemy.exc.ProgrammingError:(psycopg2.errors.UndefinedColumn)列“關(guān)系“書籍”的isbn不存在第1行:插入書籍(isbn,標(biāo)題,作者,出版年份)VAL ...PYTHON CODE: import csvimport osfrom flask import Flask, render_template, requestfrom models import *from application import DATABASE_URLapp = Flask(__name__)app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URLapp.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = Falsedb.init_app(app)def main(): f = open("books.csv") reader = csv.reader(f, delimiter=',') next(reader) # skips the top line for isbn, title, author, date in reader: book = Book(isbn = isbn, title = title, author = author, publicationyear = int(date)) db.session.add(book) print(f"Added book with {isbn}, title : {title}, author: {author}, publication year: {date}.") print("commiting") db.session.commit() print("committed all data")if __name__ == "__main__": with app.app_context(): main()
1 回答

蝴蝶刀刀
TA貢獻1801條經(jīng)驗 獲得超8個贊
看來您的模型文件可能有問題。錯誤是books
說不存在,你確定你已經(jīng)創(chuàng)建了你的 books 表嗎?db.create_all()
您的模型應(yīng)該位于表定義所在的位置。我建議您在開始使用 Python-Flask-SQLAlchemy 之前熟悉使用 SQL 命令。希望這可以幫助。
添加回答
舉報
0/150
提交
取消