On 8/09/2018 9:09 PM, Jae Pil Choi wrote:
I'm asking this on the official Django community after I asked on
Stackoverflow because I still don't know what is the best practice for
this particular action.
(Original Question on Stackoverflowhere
<https://stackoverflow.com/questions/52225453/what-is-django-commend-equivalent-to-rails-rails-db-drop/52226394?noredirect=1#comment91407548_52226394>:
What is Django commend equivalent to Rails' $ rails db drop)
My scenario goes as follow:
1. I create 2 model class objects, Post, Product in models.py
2. Post has 2 attributes: post_title, post_text. Product has its own
but that's irrelevant.
3. I make migrations and migrate.
4. Then I go to /admin of my page and add some rows both on Post and
Product.
5. Now I remember that I I forgot to add an attribute post_author on
Post and adds it in models.py
6. If I make migrations, Django warns me that already existing rows
needs default values since they don't have this new attributes.
7. I don't want any post without an author so I want to drop this
existing rows and make it all again.
8. However, I don't want to lose any Product rows already created.
Here's the question: How do I drop ONLY Post table leaving Product and
my Superuser intact?
You need a custom migration ... ./migrations/remove_authorless_rows.py
def forwards_func(apps, schema_editor):
Model_class = apps.get_model("<app name>", "Model_class")
db_alias = schema_editor.connection.alias
Model_class.objects.using(db_alias).filter(author=None).delete()
class Migration(migrations.Migration):
dependencies = [
('<app name>', '<most recent previous successful migration>'),
]
operations = [
migrations.RunPython(
forwards_func,
),
]
This is off the top of my head so you would need to test it in a sandbox
and repair as required.
Original Stackoverflow post suggests manually removing db.sqlite3 file
and migrations files but I really don't think this is the way to go
and there must be some best practice or commend for this since Django
is already at 2.1.
If not, I think there must be a good reason not to have one and I want
to know why.
--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to django-users+unsubscr...@googlegroups.com
<mailto:django-users+unsubscr...@googlegroups.com>.
To post to this group, send email to django-users@googlegroups.com
<mailto:django-users@googlegroups.com>.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/a8170e3d-ae7f-4b59-9330-fef33fb9816d%40googlegroups.com
<https://groups.google.com/d/msgid/django-users/a8170e3d-ae7f-4b59-9330-fef33fb9816d%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-users/b22a3574-4616-23fb-1d5a-f29c20cdd571%40dewhirst.com.au.
For more options, visit https://groups.google.com/d/optout.