-1

So during migration I ran into an error saying I don't have a certain column. I've revised my code but I keep getting the same error and realized that I need to drop the database itself (tried deleting all migrations files except init.py). But I can't figure out how.

FYI, the project name is mvp, and the app where the problem occurred is qanda. I want to preserve the db in other apps in my project.

HOW DO I DROP A SPECIFIC DATABASE? Thank you very much.

Ryan Oh
  • 597
  • 5
  • 21
  • Does this answer your question? [PostgreSQL: Drop PostgreSQL database through command line](https://stackoverflow.com/questions/7073773/postgresql-drop-postgresql-database-through-command-line) – JPG Jul 31 '20 at 08:17
  • Please make sure you actually want to do the database. By default in Django, you well have one database four the entire project. From your description it sounds like you want to drop a single table instead. – Alasdair Jul 31 '20 at 08:24
  • @Alasdair Okay let me get this straight. So I have about four application in a single project. And in each of them there I have several models. For example, I have an application in my project named 'Qanda', and have three models. I'm trying to delete the entire database IN Qanda. Or did I misunderstand the db structure of Django? Thanks. – Ryan Oh Jul 31 '20 at 08:27
  • Your databases are defined in `settings.py`. Assuming you have `DATABASES = {'default': {'ENGINE': 'django.db.backends.postgresql', 'NAME': '...', ...}}`, with a single item `default`, then you only have one database in your project. – Alasdair Jul 31 '20 at 08:37
  • Deleting migration files or running `migrate` with `--fake` are both dangerous, because they can cause the database to get out of sync with the migration files, getting them back in sync can be tricky and time consuming, so you might not find anyone on Stack Overflow who can help you do it. – Alasdair Jul 31 '20 at 08:44
  • Since you have deleted all of your migrations in the `qanda` app, you might be able to resolve the issue by deleting the tables from that app. Normally, they'll start with `qanda_...`. **First, back up any data that is important** before you run any of these destructive commands from a random person on the internet who hasn't seen your code or database tables. – Alasdair Jul 31 '20 at 08:45
  • @Alasdair Thank you for the detailed answer. I don't have any important data in the ```qanda``` table at the moment. Where do I find the tables named ```qanda_...```? – Ryan Oh Jul 31 '20 at 08:47
  • Then, you can open up a database shell with `python manage.py dbshell`. You can list tables with `\d` in postgresql. By default, tables will start with the app name, so a command to drop a table might be `drop table qanda_question;`. You might need to drop tables in the correct order if there are foreign keys. There is a CASCADE option but I'd avoid that because it could delete tables you weren't expecting. – Alasdair Jul 31 '20 at 08:50
  • *I don't have any important data* - sure, but I'm recommending you to back up in case you accidentally drop data in other tables that is important. – Alasdair Jul 31 '20 at 08:52
  • If there aren't any foreign keys from other apps to the `qanda` models, then dropping the qanda tables, deleting the qanda migrations, running makemigrations and then migrate will hopefully fix the problem. If so, you've been lucky this time. Don't delete migrations you have already applied or use `--fake` unless you are sure what you're doing, it's not always this simple to fix so my recommendation is usually to drop the entire database (including the tables from all the other apps) and start again. – Alasdair Jul 31 '20 at 08:54
  • @Alasdair I don't know what to say. Your solution as well as advice helped so much. I'll keep you advice in mind. Thank you very much. – Ryan Oh Jul 31 '20 at 08:59
  • @Alasdair Oh one last question: So I've deleted ```qanda_question``` as well as qanda migrations. Then I ran makemigrations and migrate. Everything worked fine up until here. But then, when I tried to access through admin, I got this error ```relation "qanda_question" does not exist```. I thought the table is automatically made in the migration process. What do you think went wrong here? Thank you again, very much. – Ryan Oh Jul 31 '20 at 09:10
  • @Alasdair Yes. Since I've deleted all the migration files and re-did makemigrations and migrate, I have ```0001_initial.py``` file, and inside it there is code that creates model named ```Question```. And I do have migrated it. I don't see what went wrong... – Ryan Oh Jul 31 '20 at 09:33
  • What did `migrate` output when you ran that migration (for the first time)? – Alasdair Jul 31 '20 at 09:42
  • @Alasdair Umm ```Migrations for 'qanda': qanda/migrations/0001_initial.py - Create model QuestionCategory - Create model Question - Create model Answer``` – Ryan Oh Jul 31 '20 at 09:43
  • @Alasdair Oh it looks terrible. But anyway, it says all three models are created. But when I click on the ```Quesion``` model in Django admin, I get the error that ```qanda_question does not exist```. As well, I can't see ```qanda_question``` in the dbshell. – Ryan Oh Jul 31 '20 at 09:45
  • @Alasdair This is so frustrating. – Ryan Oh Jul 31 '20 at 09:45
  • *Migrations for 'qanda':* - that's from when you ran `makemigrations`, not when you ran `migrate`. – Alasdair Jul 31 '20 at 09:52
  • @Alasdair Oh right. Sorry. ```Operations to perform: Apply all migrations: qanda Running migrations: No migrations to apply.``` This is what I got. Now that I see it, it's strange that there is no migration to apply. – Ryan Oh Jul 31 '20 at 09:59
  • I missed out a step before. After deleting all the `qanda` tables, you need to run `python manage.py migrate qanda zero --fake`, to tell Django that no migrations for `qanda` have been applied yet. – Alasdair Jul 31 '20 at 10:05
  • @Alasdair Oh I see. Let me try and get back. I appreciate it very, very much. – Ryan Oh Jul 31 '20 at 10:07
  • @Alasdair Oh one more thing please. Do I have to delete the sequence files as well? – Ryan Oh Jul 31 '20 at 10:14
  • When you run the migration, it will try to create the sequences. If that causes an error when the sequences already exist then yes, you'll have to drop the sequences as well. – Alasdair Jul 31 '20 at 10:32
  • 1
    @Alasdair Wow it worked. Everything's fine now. I've been so frustrated with this for hours and now I'm relieved!!! I can't thank you enough. – Ryan Oh Jul 31 '20 at 10:40

1 Answers1

1

Probably late to the party but you can change the db name in your settings file then makemigrations & migrate again...for a clean slate. I used to do that when uncertain of the changes impact in my test environment. DATABASES = {'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'NewDatabeseName', # the rest of the config here... } }

At a later stage you can go in and cleanup the unused db(s)