如何在flask sqlalchemy中将一个表中的外键元素的值添加到另一个表中?

时间:2016-02-01 17:23:02

标签: python flask-sqlalchemy

    class store_ques(Resource):
    def post(self):
        qargs = qparser.parse_args()
        uname = qargs['username']
        for uid in session.query(user.uid).filter(user.username == uname):
            quid=uid

        ques_data = {'uid' : quid, 'question' : qargs['question']}
        quser = questions(uid = ques_data['uid'], question = ques_data['question'])
        session.add(quser)
        session.commit()
        return ques_data

    api.add_resource(store_ques, '/ques')

问题是一个具有来自用户类的外键字段uid的类。当我这样做时,我得到了完整性错误。

我使用的

命令:

curl http://127.0.0.1:5000/ques -H 'Content-Type: application/json' -d '{"username":"man","question":"zkjdhkjskjdhskhsk sjdkjsdnn"}' -X POST -v

输出:

IntegrityError: (IntegrityError) (1452, 'Cannot add or update a child row: a foreign key constraint fails (`myop`.`ques_bank`, CONSTRAINT `ques_bank_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `user_info` (`uid`))') 'INSERT INTO ques_bank (uid, question) VALUES (%s, %s)' (('c29f8aad-8963-4b93-ae70-c8deab1b',), u'zkjdhkjskjdhskhsk sjdkjsdnn')

任何帮助表示赞赏。我是新手,我正在努力学习这一点。

0 个答案:

没有答案