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 bug so models with BigBitFields can be bulk created and updated #2743

Closed
wants to merge 1 commit into from

Conversation

andycasey
Copy link

Any model with a BigBitField cannot be created in bulk using Model.bulk_create(...) or updated in bulk using Model.bulk_update(...). This PR fixes that.

Here is a minimum reproducible example that was tested with PostgreSQL and peewee 3.15.2:

from peewee import *

database = ...

class MRE(Model):

    pk = AutoField()
    flags = BigBitField(null=True)

    class Meta:
        database = database

with database.atomic():
    database.create_tables([MRE])

# This works:
first_obj = MRE.create()
second_obj = MRE.create()

# This works:
for i in range(1, 10):
    first_obj.flags.set_bit(i)
    second_obj.flags.set_bit(i)

# This works:
first_obj.save()
second_obj.save()

for i in range(10, 20):
    first_obj.flags.set_bit(i)
    second_obj.flags.set_bit(i)

# This works:
first_obj.save()

# This fails:
with database.atomic():
    MRE.bulk_update([second_obj], fields=[MRE.flags])

# This also fails:
with database.atomic():
    MRE.bulk_create([MRE(), MRE(), MRE()])

With this PR, the failures disappear 🪄

coleifer added a commit that referenced this pull request Jun 14, 2023
This fixes issues coercing values to appropriate db-value.

Refs #2743
@coleifer
Copy link
Owner

I decided instead to handle this by providing an override on the BigBitFieldData.__bytes__ method.

See f06d61e

@coleifer coleifer closed this Jun 14, 2023
gshmu pushed a commit to gshmu/peewee that referenced this pull request Aug 22, 2023
This fixes issues coercing values to appropriate db-value.

Refs coleifer#2743
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.

2 participants