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

Doesn't duplicate with custom id #9

Closed
gerbyzation opened this issue Nov 4, 2019 · 2 comments · Fixed by #10
Closed

Doesn't duplicate with custom id #9

gerbyzation opened this issue Nov 4, 2019 · 2 comments · Fixed by #10

Comments

@gerbyzation
Copy link
Contributor

A model with a custom id field is not duplicated:

the Branch model has the id id = models.UUIDField(primary_key=True, default=uuid4)

>>> base_branch = Branch.objects.create(name='Main branch', parent=None, is_master=True, status=Branch.DRAFT)
>>> base_branch.pk
UUID('7a613f9d-90fe-4116-b897-69505cd4ff5c')
>>> clone = base_branch.make_clone()
>>> clone.pk
UUID('7a613f9d-90fe-4116-b897-69505cd4ff5c')

I think this is because the custom id shows up in _meta.concrete_fields the value is copied over, preventing it from generating a new PK.

>>> base_branch._meta.concrete_fields
(<django.db.models.fields.UUIDField: id>, <django.db.models.fields.CharField: name>, <django.db.models.fields.BooleanField: is_master>, <django.db.models.fields.related.ForeignKey: parent>, <django.db.models.fields.CharField: status>)

The primary key field can be found through instance._meta.pk, filtering this out of the fields to copy should resolve this problem I think.

@jackton1
Copy link
Member

jackton1 commented Nov 5, 2019

@all-contributors please add @gerbyzation for bug, tests and code

@allcontributors
Copy link
Contributor

@jackton1

I've put up a pull request to add @gerbyzation! 🎉

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 a pull request may close this issue.

2 participants