-
Notifications
You must be signed in to change notification settings - Fork 1
fix(migration): make migration compatible with django 3 and 4 #10
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '1.0.2' | ||
__version__ = '1.0.3' |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,21 +3,37 @@ | |
from django.db import migrations, models | ||
|
||
|
||
def remove_duplicated_records(apps, schema_editor): | ||
IdempotencyKey = apps.get_model('rest_framework_idempotency_key', 'IdempotencyKey') | ||
existed_keys = set() | ||
for id_key in IdempotencyKey.objects.all(): | ||
if id_key.idempotency_key in existed_keys: | ||
id_key.delete() | ||
Comment on lines
+9
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 2 questions:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, in most programming languages (even in Python), you shouldn't remove any item from a list during the iteration. It's because the removal changes the list itself. I'm not sure if the django orm query support this operation.
what if other columns are different? why not remove both? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
要做到他說的事情 要走類似下面的方式 不過新版的 python 會噴就是了
這種手法interpreter就判斷不出來了,但是我們要操作的會是 container 本身 (list, dict, set, ...),不是裡面的 item
|
||
else: | ||
existed_keys.add(id_key.idempotency_key) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('rest_framework_idempotency_key', '0002_alter_idempotencykey_user'), | ||
] | ||
|
||
operations = [ | ||
migrations.RemoveField( | ||
migrations.RunPython(remove_duplicated_records, migrations.RunPython.noop), | ||
migrations.AlterField( | ||
model_name='idempotencykey', | ||
name='user', | ||
field=models.IntegerField(null=True), | ||
), | ||
migrations.RemoveConstraint( | ||
model_name='idempotencykey', | ||
name='Unique IdempotencyKey (user, idempotency_key)', | ||
), | ||
migrations.RemoveField( | ||
model_name='idempotencykey', | ||
name='user', | ||
), | ||
migrations.AddConstraint( | ||
model_name='idempotencykey', | ||
constraint=models.UniqueConstraint( | ||
|
Uh oh!
There was an error while loading. Please reload this page.