我正在构建Flask应用,并且我的models.py中有两个表。我试图用外键将它们连接起来,但是我不断收到以下错误消息:
psycopg2.DataError: invalid input syntax for integer: "" WHERE toys.toy_id = ' '
我以为我遵循了文档,但显然不行。
这是我的模型。py:
class User(UserMixin, db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
...
toys = db.relationship('Toy', backref='user', lazy='dynamic')
def __init__(self, toys=''):
self.toys = toys
class Toy(UserMixin, db.Model):
__tablename__ = 'toys'
id = db.Column(db.Integer, primary_key=True)
...
toy_id = db.Column(db.Integer, db.ForeignKey('users.id'))
def __init__(self, toy_id=''):
self.toy_id = toy_id
这是我的server.py文件:
@app.route("/api/toy", methods=["GET", "POST"])
def toy():
incoming = request.get_json()
toy = Toy.query.filter_by(toy_id=incoming["toy_id"]).first()
最后一行是生成错误的行。我在这里想念东西吗?