我很难理解为什么我的Django数据库查询失败。
我的Django查询如下所示:
the_firebaseUID = self.request.query_params.get('firebaseUID', None)
this_user_profile = UserProfile.objects.filter(firebaseUID=the_firebaseUID)
这将生成以下查询:
SELECT `userprofile_userprofile`.`id`, `userprofile_userprofile`.`user_id`, `userprofile_userprofile`.`firebaseUID` FROM `userprofile_userprofile` WHERE `userprofile_userprofile`.`firebaseUID` = 4929e406-9d75-43e2-afa4-fe641f3e85f5
我的模特是:
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
firebaseUID = models.CharField(max_length=100)
有一个值为4929e406-9d75-43e2-afa4-fe641f3e85f5
MySQL对数据库运行查询时出现以下错误:
11:03:51 SELECT `userprofile_userprofile`.`id`, `userprofile_userprofile`.`user_id`, `userprofile_userprofile`.`firebaseUID` FROM `userprofile_userprofile` WHERE `userprofile_userprofile`.`firebaseUID` = 4929e406-9d75-43e2-afa4-fe641f3e85f5 LIMIT 0, 1000 Error Code: 1367. Illegal double '4929e406' value found during parsing 0.198 sec
答案 0 :(得分:0)
我会说你的the_firebaseUID
变量保存为double。你应该确保它是一个字符串。这可能有效:
this_user_profile = UserProfile.objects.filter(firebaseUID=str(the_firebaseUID))
查询应翻译成:
SELECT `userprofile_userprofile`.`id`, `userprofile_userprofile`.`user_id`, `userprofile_userprofile`.`firebaseUID` FROM `userprofile_userprofile` WHERE `userprofile_userprofile`.`firebaseUID` = `4929e406-9d75-43e2-afa4-fe641f3e85f5`