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

fix: query copying #14

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

henadzit
Copy link

@henadzit henadzit commented Oct 20, 2024

Overview

This fixes the issue where the copies of a query might share a state - QueryBuilder._on_conflict_fields. This PR should fix tortoise/tortoise-orm#1722 in tortoise-orm.

Issue

from pypika.dialects.postgresql import PostgreSQLQuery
from pypika.queries import Table


table_abc = Table("abc")
base_query = PostgreSQLQuery.into(table_abc).insert(1)

query1 = base_query.on_conflict(table_abc.id).do_nothing()
print("query1 before the issue:", query1)

# this introduces an issue
query2 = base_query.on_conflict(table_abc.id).do_nothing()
print("query1:", query1)
print("query2:", query2)

This will print

query before the issue INSERT INTO "abc" VALUES (1) ON CONFLICT ("id") DO NOTHING
query1 INSERT INTO "abc" VALUES (1) ON CONFLICT ("id", "id") DO NOTHING
query2 INSERT INTO "abc" VALUES (1) ON CONFLICT ("id", "id") DO NOTHING

Please note ... ON CONFLICT ("id", "id") ... after the second query was created!

Here's the code in tortoise-orm which is affected by this issue
https://github.com/tortoise/tortoise-orm/blob/0ddf8d327949be3ad812c41e4215c85b997214b9/tortoise/queryset.py#L1882

@waketzheng
Copy link
Contributor

@henadzit Cloud you help to review this PR: #10

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

Successfully merging this pull request may close these issues.

Bulk creation with on_conflict fails with CharField PK
2 participants