Django数据库查询在比较字符串时给出了意外的“非法双重”

时间:2021-02-16 20:27:58

I am struggling to understand why my Django database query is failing.

我很难理解为什么我的Django数据库查询失败了。

My Django query looks like this:

我的Django查询如下所示:

        the_firebaseUID = self.request.query_params.get('firebaseUID', None)
        this_user_profile = UserProfile.objects.filter(firebaseUID=the_firebaseUID)

This generates the following query:

这会生成以下查询:

SELECT `userprofile_userprofile`.`id`, `userprofile_userprofile`.`user_id`, `userprofile_userprofile`.`firebaseUID` FROM `userprofile_userprofile` WHERE `userprofile_userprofile`.`firebaseUID` = 4929e406-9d75-43e2-afa4-fe641f3e85f5

My model is:

我的模型是:

class UserProfile(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    firebaseUID = models.CharField(max_length=100)

There is a firebaseUID field entry with the value 4929e406-9d75-43e2-afa4-fe641f3e85f5

有一个firebaseUID字段条目,值为4929e406-9d75-43e2-afa4-fe641f3e85f5

MySQL gives the following error when running the query against the database:

在针对数据库运行查询时,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

1 个解决方案

#1


0  

I would say that your the_firebaseUID variable is saving as double. You should ensure that it is a string. This may work:

我会说你的the_firebaseUID变量保存为double。你应该确保它是一个字符串。这可能有效:

this_user_profile = UserProfile.objects.filter(firebaseUID=str(the_firebaseUID))

And the query should be translated to:

并且查询应该转换为:

SELECT `userprofile_userprofile`.`id`, `userprofile_userprofile`.`user_id`, `userprofile_userprofile`.`firebaseUID` FROM `userprofile_userprofile` WHERE `userprofile_userprofile`.`firebaseUID` = `4929e406-9d75-43e2-afa4-fe641f3e85f5`

#1


0  

I would say that your the_firebaseUID variable is saving as double. You should ensure that it is a string. This may work:

我会说你的the_firebaseUID变量保存为double。你应该确保它是一个字符串。这可能有效:

this_user_profile = UserProfile.objects.filter(firebaseUID=str(the_firebaseUID))

And the query should be translated to:

并且查询应该转换为:

SELECT `userprofile_userprofile`.`id`, `userprofile_userprofile`.`user_id`, `userprofile_userprofile`.`firebaseUID` FROM `userprofile_userprofile` WHERE `userprofile_userprofile`.`firebaseUID` = `4929e406-9d75-43e2-afa4-fe641f3e85f5`