使用 flask-sqlalchemy 在 SQLite 数据库模型中将多个项目提交到一个 table 时出错
Error committing multiple items to one table in SQLite DB model with flask-sqlalchemy
添加一个提交时 - db.session.add(new_professor)
程序正确执行,添加第二个提交时 db.session.add(prof_hindex)
这是 错误:
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: professors.name
[SQL: INSERT INTO professors (name, hindex, date_created) VALUES (?, ?, ?)]
[parameters: (None, 25, '2022-03-20 21:14:39.624208')]
似乎一旦我尝试将两项提交到 table,name = db.Column(db.String(100), nullable=False)
为空。
数据库模型
class Professors(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
hindex = db.Column(db.Integer)
date_created = db.Column(db.DateTime, default=datetime.utcnow)
# Return a string when adding to DB
def __repr__(self):
return f"Professors('{self.name}','{self.hindex}')"
POST 路线
name = request.form.get("name")
if not name:
return render_template("failure.html")
# Google Scholar API
search_query = scholarly.search_author(name)
author = next(search_query)
indices_list = (scholarly.fill(author, sections=['indices']))
hindex_int = indices_list['hindex']
# Passed variables to DB
new_professor = Professors(name=name)
prof_hindex = Professors(hindex=hindex_int)
db.session.add(new_professor)
db.session.add(prof_hindex)
db.session.commit()
return redirect("/registrants")
不确定为什么在table中添加两项时上面的参数显示为空?您将如何为每个 class 添加多个项目?
您混淆了一些术语。在您的情况下,说“我想添加多个项目”意味着“我想添加多个教授行”。但是查看您的代码,您似乎实际上是在问“添加单个 Professor 行时如何添加多个属性”。
要添加具有多个属性的单个教授:
...
# Passed variables to DB
new_professor = Professors(
name=name,
hindex=hindex_int)
db.session.add(new_professor)
db.session.commit()
return redirect("/registrants")
添加多位教授(行):
professorA = Professors(
name=nameA,
hindex=hindex_intA)
professorB = Professors(
name=nameB,
hindex=hindex_intB)
db.session.add(professorA)
db.session.add(professorB)
db.session.commit()
return redirect("/registrants")
您遇到错误,因为您试图添加第二位教授(行)prof_hindex = Professors(hindex=hindex_int)
,但您没有提供 name
属性,由于 nullable=False
.
添加一个提交时 - db.session.add(new_professor)
程序正确执行,添加第二个提交时 db.session.add(prof_hindex)
这是 错误:
sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError) NOT NULL constraint failed: professors.name [SQL: INSERT INTO professors (name, hindex, date_created) VALUES (?, ?, ?)]
[parameters: (None, 25, '2022-03-20 21:14:39.624208')]
似乎一旦我尝试将两项提交到 table,name = db.Column(db.String(100), nullable=False)
为空。
数据库模型
class Professors(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
hindex = db.Column(db.Integer)
date_created = db.Column(db.DateTime, default=datetime.utcnow)
# Return a string when adding to DB
def __repr__(self):
return f"Professors('{self.name}','{self.hindex}')"
POST 路线
name = request.form.get("name")
if not name:
return render_template("failure.html")
# Google Scholar API
search_query = scholarly.search_author(name)
author = next(search_query)
indices_list = (scholarly.fill(author, sections=['indices']))
hindex_int = indices_list['hindex']
# Passed variables to DB
new_professor = Professors(name=name)
prof_hindex = Professors(hindex=hindex_int)
db.session.add(new_professor)
db.session.add(prof_hindex)
db.session.commit()
return redirect("/registrants")
不确定为什么在table中添加两项时上面的参数显示为空?您将如何为每个 class 添加多个项目?
您混淆了一些术语。在您的情况下,说“我想添加多个项目”意味着“我想添加多个教授行”。但是查看您的代码,您似乎实际上是在问“添加单个 Professor 行时如何添加多个属性”。
要添加具有多个属性的单个教授:
...
# Passed variables to DB
new_professor = Professors(
name=name,
hindex=hindex_int)
db.session.add(new_professor)
db.session.commit()
return redirect("/registrants")
添加多位教授(行):
professorA = Professors(
name=nameA,
hindex=hindex_intA)
professorB = Professors(
name=nameB,
hindex=hindex_intB)
db.session.add(professorA)
db.session.add(professorB)
db.session.commit()
return redirect("/registrants")
您遇到错误,因为您试图添加第二位教授(行)prof_hindex = Professors(hindex=hindex_int)
,但您没有提供 name
属性,由于 nullable=False
.