Skip to content

Conversation

charettes
Copy link
Member

Trac ticket number

ticket-27222

@charettes charettes marked this pull request as ready for review March 22, 2025 04:48
@charettes
Copy link
Member Author

The unveiled failures are related to this ticket

TL;DR the documented pattern for copying model instances breaks when some fields are deferred on the original instance.

This poses a problem with this patch as it marks any field assigned a database expression at save time, db_default included, as deferred instead of leaving a dangling expression assigned (a DatabaseDefault instance in the case of db_default).

@charettes
Copy link
Member Author

This is now based on #19445

Comment on lines +1149 to +1158
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an explicit literal value is provided for a field there is no need to return it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except that I've written some code like this for MongodB where the id is ObjectIdAutoField: Question.objects.create(id="000000000000000000000013"). Previously, this would refresh id to the returned value, ObjectId("000000000000000000000013"). What do you think?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the same problem might happen with some form of UUID field that returns uuid.UUID instances I guess. Could you write a regression test for it and file an issue?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I imagine UUIDAutoField (ticket-32577) could reproduce it. Do you have a more generic way in mind? I could try for some minimal implementation of UUIDFIeld + django.contrib.postgres.functions.RandomUUID to create a regression test in postgres_tests but this might be unnecessarily complicated in light of the MongoDB tests.

This works as a regression test, at least on SQLite and PostgreSQL. I'm unsure if all databases will accept it.

a = AutoModel.objects.create(pk="1")
self.assertEqual(a.pk, 1)

Copy link
Member Author

@charettes charettes Sep 16, 2025