[英]PeeWee: Ignores foreign key constraint
Job.select().where(Job.user == current_user.id and Job.status == "Running")
Keeps returning the wrong row from the wrong user. 不断从错误的用户返回错误的行。 It seems to ignore the statement
Job.user == current_user.id
altogether. 似乎完全忽略了语句
Job.user == current_user.id
。
I also tried 我也试过
Job.select().join(User).where(Job.status == "Running")
same thing, it won't return the correct Job belonging to the current user. 同样,它不会返回属于当前用户的正确Job。
The problem here is that and
doesn't translate to SQL AND
. 这里的问题是,
and
没有转化为SQL AND
。
In fact, it can't do anything like that, in any library. 实际上,它在任何库中都无法做到这一点。 The
and
operator can't be overloaded, it short-circuits (doesn't evaluate the second argument) if the first isn't truthy, and it always returns the last thing it evaluated. and
运算符不能重载,如果第一个不正确,它将短路(不评估第二个参数),并且它始终返回所评估的最后一个东西。
So, since Job.user == current_user.id
returns a non-empty query object, which is truthy, Job.user == current_user.id and Job.status == "Running"
returns Job.status == "Running"
. 因此,由于
Job.user == current_user.id
返回一个非空查询对象,这是事实, Job.user == current_user.id and Job.status == "Running"
返回Job.status == "Running"
。 Which is why it's ignoring the current_user.id
. 这就是为什么它忽略了
current_user.id
。
Use the &
operator, or the bin_and
method, as the docs say. 如文档所述,使用
&
运算符或bin_and
方法。
Also, remember that &
doesn't have the same precedence as and
, so you will need parens around each comparison. 另外,请记住,
&
的优先级与and
优先级不同,因此在每次比较时都需要加括号。
As for your second attempt, that join
works fine—it just means that each row in Job
is joined up with the columns from the corresponding row in User
. 对于您的第二次尝试,该
join
工作正常-只是意味着Job
中的每一行都与User
相应行中的列相连。 You haven't even tried to tell it what user you want restrict it to, and it can't read your mind. 您甚至都没有试图告诉它要限制它的用户,并且它无法读懂您的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.