tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

Reverse to contains/icontains

Open kagamir opened this issue 3 years ago • 3 comments

In this question was solved problem for reverse LIKE operation in SQL, for example if field name is "Peter Johnson", we could find it by such query:

select name from user where "Mr. Peter Johnson" like CONCAT('%', name, '%')

Is there any way to do such thing in tortoise?

kagamir avatar Sep 13 '22 11:09 kagamir

await User.annotate(name=Concat(...)).fiter(name__contains='xxx')

long2ice avatar Sep 13 '22 12:09 long2ice

Thanks reply.

User.annotate(cname=Concat("name")).fiter(cname__contains="Mr. Peter Johnson").sql()

its returns:

SELECT `name`, CONCAT(`name`) `cname` FROM `user` WHERE CAST(CONCAT(`name`) AS CHAR) LIKE '%Mr. Peter Johnson%'

It finds who contains this string("Mr. Peter Johnson"). However, I want a name that appears inside a string. The string should be to the left of LIKE i think.

kagamir avatar Sep 14 '22 03:09 kagamir

Try use source_field in field because Mr. Peter Johnson is invalid python val

long2ice avatar Sep 14 '22 03:09 long2ice