#QuerySet.update() returning SQL errors.

11 messages · Page 1 of 1 (latest)

tawdry otter
#

When using QuerySet.update() I am getting an SQL error 1443 “The definition of the table ‘U0’ prevents operation UPDATE”. I have succeeded in doing the update on the SQL database itself logged in as the correct user, so am unsure what could be causing this issue. Does anyone have some suggestions?

last raptor
#

Can you show the update you're doing with the raw sql versus the update you're doing with the queryset please?

tawdry otter
#

@last raptor here it is:
UPDATE function_success_tracker SET queue = 1 WHERE pyfunction = “some_value”

functions_to_run = selected_rows.values("pyfunction").distinct()
query_set = FunctionSuccessTracker.objects.filter(pyfunction__in=functions_to_run)
query_set.update(queue="1")

barren tulip
#

But this will run on all rows?

#

Unless pyfunction can be NULL, possibly?

#

Or no. Because you start with a (i assume) sunset. But it will potentially run on more rows than selected rows 🤔

last raptor
#

Do you mind running the following in the python shell?

>>> from django.db import connection
>>> connection.queries_log.clear() 
>>> # Run your code until and including update
...
>>> print(connection.queries)
#

The purpose of running this, is to see what django is running versus what you're manually running. You should be able to spot the issue.

tawdry otter
#

It is fine if it updates multiple rows, it is intended to.

A better version of the SQL would be UPDATE function_success_tracker SET queue = 1 WHERE pyfunction IN (some, list)

tawdry otter
last raptor
#

Precisely, you could also copy that and run it to test.