Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OperationalError: syntax error at or near "LEFT" #1081

Closed
lifr0m opened this issue Mar 6, 2022 · 2 comments
Closed

OperationalError: syntax error at or near "LEFT" #1081

lifr0m opened this issue Mar 6, 2022 · 2 comments

Comments

@lifr0m
Copy link

lifr0m commented Mar 6, 2022

Describe the bug
When I try to filter model by foreign key's column value and update, I get this error.

To Reproduce
bug_models.py

from tortoise import Model, fields


class Email(Model):
    value = fields.CharField(255)

    class Meta:
        table = 'emails'


class User(Model):
    email = fields.OneToOneField('models.Email', related_name='user')

    class Meta:
        table = 'users'

main.py


from tortoise import Tortoise
from bug_models import User, Email


async def main():
    await Tortoise.init(
        db_url='postgres://localhost/bugs',
        modules={
            'models': ['bug_models']
        }
    )
    await Tortoise.generate_schemas()

    email = await Email.create(value='example@example.com')
    await User.create(email=email)

    await Email.filter(user__id=1).update(value='right_example@example.com')


asyncio.run(main())

Expected behavior
Update all Email records where their user's id is 1

Additional context
...

@druid8
Copy link

druid8 commented Mar 9, 2022

I have same issue, it seems that SQL produced for update is invalid when there is also a left join.
I grabbed generated SQL for my case, the left outer join clause should be after SET not before.

UPDATE "lampdescriptor" 
  LEFT OUTER JOIN "peripheraldescriptor" "lampdescriptor__descriptor" 
    ON "lampdescriptor__descriptor"."id"="lampdescriptor"."descriptor_id" 
SET "last_func_test_time"='2022-03-09 10:10:27.672996+00:00',"last_func_test_result"=0 
WHERE "lampdescriptor__descriptor"."peripheral_id"=2

the corresponding Tortoise lookup is:

        await LampDescriptor.filter(descriptor__peripheral_id=peripheral.peripheral_id).update(
            last_func_test_time=msg.timestamp,
            last_func_test_result=msg.result
        )

I have LampDescriptor model with OneToOne to Descriptor model (through descriptor field). Descriptor model has a peripheral_id column.

@long2ice
Copy link
Member

long2ice commented Jun 9, 2022

Fixed in latest code, just wait next release

@long2ice long2ice closed this as completed Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants