Flask SQLAlchemy - 与两个模型的关系
Flask SQLAlchemy - Relationship with two models
我正在尝试创建可以跟踪所有服务事件的应用程序。
我创建了两个模型
- 打印机 - 包含有关打印机的所有信息(库存编号、IP 地址等)
- PrinterService - 它应该保存所有关于服务的数据。
我正在尝试连接这两者,所以如果我填写服务表格 - 它将连接到打印机 table.. 但我失败了。
这就是我正在使用的。
型号:
class Printer(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200))
ip = db.Column(db.String(200))
class PrinterServis(db.Model):
id = db.Column(db.Integer, primary_key=True)
printer_komponent = db.Column(db.String(140))
printer_opis_chyby = db.Column(db.String(140))
printer_id = db.Column(db.Integer, db.ForeignKey('printer.id'))
表格:
class PrinterForm(FlaskForm):
printer_name = StringField('Názov Tlačiarne', validators=[InputRequired()])
printer_ip = StringField('IP Tlačiarne', validators=[InputRequired()])
class PrinterServisForm(FlaskForm):
printer_komponent = StringField('Vadný komponent', validators=[InputRequired()])
printer_opis_chyby = StringField('Opis Chyby', validators=[InputRequired()])
观看次数:
@app.route('/')
def index():
printers = Printer.query.all()
return render_template('index.html', printers=printers)
@app.route('/add', methods=['GET', 'POST'])
def add_printer():
form = PrinterForm()
if form.validate_on_submit():
name = form.printer_name.data
ip = form.printer_ip.data
new_printer = Printer(name=name, ip=ip)
db.session.add(new_printer)
db.session.commit()
flash("Pridanie tlačiarne prebehlo úspešne")
return redirect(url_for('index'))
return render_template('add_printer.html', form=form)
@app.route('/printer/<int:printer_id>/', methods=['GET', 'POST'])
def view_printer(printer_id):
form = PrinterServisForm()
printer = Printer.query.get_or_404(printer_id)
if form.validate_on_submit():
printer_komponent = form.printer_komponent.data
printer_opis_chyby = form.printer_opis_chyby.data
printer_servis_id = Printer.query.get_or_404(printer_id)
new_servis = PrinterServis(printer_komponent=printer_komponent, printer_opis_chyby=printer_opis_chyby, printer_id=printer_servis_id)
db.session.add(new_servis)
db.session.commit()
flash("Servis bol objednaný")
return redirect(url_for('index'))
return render_template('printer.html', printer=printer, form=form)
有了这个我得到了错误
sqlalchemy.exc.InterfaceError: (sqlite3.InterfaceError) Error binding parameter 2 - probably unsupported type.
[SQL: INSERT INTO printer_servis (printer_komponent, printer_opis_chyby, printer_id) VALUES (?, ?, ?)]
[parameters: ('Valec', 'Valec je poškodený, nefunguje', <Printer 1>)]
(Background on this error at: https://sqlalche.me/e/14/rvf5)
问题可能是“printer_id”的值是 而不是 1,但我该如何更改它?
我现在头疼..我只是一个初学者,很抱歉可能是愚蠢的问题。
谢谢!
你的问题是你的两个模型之间的关系不是很完整。您需要在 Printer
模型中添加类似于此的内容来完成关系:
printer_service = db.relationship('PrinterServis', backref='printer')
这会将包含父打印机的关系 'column' 插入到 PrinterServis
table 中。要正确创建 PrinterServis
对象,请填充此 'printer' 属性而不是外键 'printer_id'.
new_servis = PrinterServis(..., printer=printer_servis_id)
然后您可以使用类似于以下内容的内容访问此父打印机:
printer_servis_object = PrinterServis.query.first() #get a valid object
printer_object = printer_servis_object.printer #access its backref of 'printer'
我正在尝试创建可以跟踪所有服务事件的应用程序。
我创建了两个模型
- 打印机 - 包含有关打印机的所有信息(库存编号、IP 地址等)
- PrinterService - 它应该保存所有关于服务的数据。
我正在尝试连接这两者,所以如果我填写服务表格 - 它将连接到打印机 table.. 但我失败了。
这就是我正在使用的。
型号:
class Printer(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200))
ip = db.Column(db.String(200))
class PrinterServis(db.Model):
id = db.Column(db.Integer, primary_key=True)
printer_komponent = db.Column(db.String(140))
printer_opis_chyby = db.Column(db.String(140))
printer_id = db.Column(db.Integer, db.ForeignKey('printer.id'))
表格:
class PrinterForm(FlaskForm):
printer_name = StringField('Názov Tlačiarne', validators=[InputRequired()])
printer_ip = StringField('IP Tlačiarne', validators=[InputRequired()])
class PrinterServisForm(FlaskForm):
printer_komponent = StringField('Vadný komponent', validators=[InputRequired()])
printer_opis_chyby = StringField('Opis Chyby', validators=[InputRequired()])
观看次数:
@app.route('/')
def index():
printers = Printer.query.all()
return render_template('index.html', printers=printers)
@app.route('/add', methods=['GET', 'POST'])
def add_printer():
form = PrinterForm()
if form.validate_on_submit():
name = form.printer_name.data
ip = form.printer_ip.data
new_printer = Printer(name=name, ip=ip)
db.session.add(new_printer)
db.session.commit()
flash("Pridanie tlačiarne prebehlo úspešne")
return redirect(url_for('index'))
return render_template('add_printer.html', form=form)
@app.route('/printer/<int:printer_id>/', methods=['GET', 'POST'])
def view_printer(printer_id):
form = PrinterServisForm()
printer = Printer.query.get_or_404(printer_id)
if form.validate_on_submit():
printer_komponent = form.printer_komponent.data
printer_opis_chyby = form.printer_opis_chyby.data
printer_servis_id = Printer.query.get_or_404(printer_id)
new_servis = PrinterServis(printer_komponent=printer_komponent, printer_opis_chyby=printer_opis_chyby, printer_id=printer_servis_id)
db.session.add(new_servis)
db.session.commit()
flash("Servis bol objednaný")
return redirect(url_for('index'))
return render_template('printer.html', printer=printer, form=form)
有了这个我得到了错误
sqlalchemy.exc.InterfaceError: (sqlite3.InterfaceError) Error binding parameter 2 - probably unsupported type. [SQL: INSERT INTO printer_servis (printer_komponent, printer_opis_chyby, printer_id) VALUES (?, ?, ?)] [parameters: ('Valec', 'Valec je poškodený, nefunguje', <Printer 1>)] (Background on this error at: https://sqlalche.me/e/14/rvf5)
问题可能是“printer_id”的值是
谢谢!
你的问题是你的两个模型之间的关系不是很完整。您需要在 Printer
模型中添加类似于此的内容来完成关系:
printer_service = db.relationship('PrinterServis', backref='printer')
这会将包含父打印机的关系 'column' 插入到 PrinterServis
table 中。要正确创建 PrinterServis
对象,请填充此 'printer' 属性而不是外键 'printer_id'.
new_servis = PrinterServis(..., printer=printer_servis_id)
然后您可以使用类似于以下内容的内容访问此父打印机:
printer_servis_object = PrinterServis.query.first() #get a valid object
printer_object = printer_servis_object.printer #access its backref of 'printer'