relation "notes_bundles" does not exist when it clearly does

Template error:
In template /home/vardhan/core/notes/templates/notes/bundles.html, error at line 30
   relation "notes_bundles" does not exist
LINE 1: ..._bundles"."created_at", "notes_bundles"."id" FROM "notes_bun...


Exception Type: ProgrammingError at /my_notes/
Exception Value: relation "notes_bundles" does not exist
LINE 1: ..._bundles"."created_at", "notes_bundles"."id" FROM "notes_bun...

this is from one of the files in migrations.py

 migrations.CreateModel(
            name='Bundles',
            fields=[
                ('title', models.TextField(max_length=100)),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('author', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
                ('notes', models.ManyToManyField(to='notes.Notes')),
            ],
            options={
                'ordering': ['-created_at'],
            },
        ),

so it does exist, I don’t know where I am going wrong.

Maybe the problem is there got pulled from github and the migrations are on my local machine and they aren’t on the production server, any way to fix that?

because the timestamp at the top of the migrations file is completely wrong wrt the production server.

I am in some deep trouble, please help me.

Hi,

can you share your Notes model? I may be wrong but I think unless you specify reverse_name manually, it will be something like notes_bundlesset - note the set at the end.

You should commit your migrations to your repository, and run manage.py migrate on your production server.

This doesn’t matter. The timestamp is just a comment for your records to determine when the migration file was generated.

1 Like

well, I did pull them from my local machine and I thought that was the problem, do you think that is fine?
I thought I shouldn’t have pulled them, but from your comment it seems like I did the right thing, why the error then?

I don’t know if you mean the entire notes/models.py or just the Notes model, so here is a link to a paste bin with all the models, there aren’t that many!
models.py
I don’t think I have setup the reverse name, so that could be the problem, have a look let me know!

Did you run migrate? If you run showmigrations does it show the right migrations have applied?

“relation does not exist” is Postgres’ way of saying the table does not exist. So it sounds like the migration did not run.

1 Like

yea, I ran into anther issue and the solution to that one said fake the migration, so I faked it. Now I regret it. here is what the showmigrations say:

notes
 [X] 0001_initial
 [X] 0002_alter_notesfiles_notes
 [X] 0003_orders
 [X] 0004_auto_20220112_1519
 [X] 0005_payment_accounts
 [X] 0006_alter_payment_accounts_cut
 [X] 0007_notes_subject
 [X] 0008_auto_20220130_2329

But I suspect they are false since I faked it. I think I will have to manually create the tables, is there a way to do it using django?

Also, I am a big fan of yours, you worked on HTMX and CORS. I love htmx, I can’t believe I am talking to you!

hey, I found this on stackoverflow, I think this is relevant, but I wanted to get your opinion before moving forward!

Reverse migrations by running back to a previous point, e.g. manage.py migrate notes 0007 will undo migration 0008. You may also need --fake when running that.

I can’t take the credit for htmx… I only made django-htmx.

1 Like

should I runt the makemigrations and migrate after that? So sorry, I am really scared to do all this, its just my first time working on a production server that I am paying for!

Yea, thats what I meant, its amazing! And the community that’s built around it on discord is amazing, I can’t express how amazing your work is! Thank you so much

Reverse and then run migrate afterwards, yes.

Back up your database before if you are worried about data loss. It’s always good to know how to do that. You can use pg_dump or a tool provided by your deployment platform.

1 Like

okay, I ran the migration and now I get,

column notes_notes.subject does not exist
LINE 1: ..._notes"."description", "notes_notes"."thumbnail", "notes_not...

I think, i need to fake a couple more migrations back and then maybe it will work, umm, I don’t know if this is helpful here but this is what is inside notes/migrations.py/0007_notes_subjects.py

class Migration(migrations.Migration):

    dependencies = [
        ('notes', '0006_alter_payment_accounts_cut'),
    ]

    operations = [
        migrations.AddField(
            model_name='notes',
            name='subject',
            field=models.CharField(blank=True, choices=[('Anatomy', 'Anatomy'), .........], max_length=50, null=True),
        ),
    ]

actually, I faked all the migrations for the notes app, is that helpful for you to know?

It’s helpful for you to know, I’m sure.

When I said:

I didn’t know what point you needed to rewind to. Reverse all the fake migrations then apply them for real.

Read up on what faked migrations mean, and inspect your db schema.

1 Like

okay, thank you so much! I will do that! You have be amazing! Thank you so much
I tried faking 0001 and now it just says:

django.db.utils.ProgrammingError: column “subject” of relation “notes_notes” does not exist

do you think I should just delete all the files in the notes/migrations and start again, I don’t have any data there, so now I can do that

So, I read a bit about it and do you think I can run,

DELETE FROM django_migrations WHERE app='notes'

in the shell and it will just let me make all the migrations again?

Faking forwards or backwards ? Be precise. Also you may want to go backwards with --fake.

If you can delet your database and start again, just do that.

Don’t delete the contents of django_migrations, that’s rarely what you want to do. Best to stick with using the migrate command to manipulate that table.

1 Like

I went backwards with

python manage.py migrate --fake notes zero

got this:

Operations to perform:
  Unapply all migrations: notes
Running migrations:
  Rendering model states... DONE
  Unapplying notes.0008_auto_20220130_2329... FAKED
  Unapplying notes.0007_notes_subject... FAKED
  Unapplying notes.0006_alter_payment_accounts_cut... FAKED
  Unapplying notes.0005_payment_accounts... FAKED
  Unapplying notes.0004_auto_20220112_1519... FAKED
  Unapplying notes.0003_orders... FAKED
  Unapplying notes.0002_alter_notesfiles_notes... FAKED
  Unapplying notes.0001_initial... FAKED

tried:

python manage.py migrate

and now I get the error:

django.db.utils.ProgrammingError: relation "notes_notes" already exists

I think that means that the notes model was already created so maybe I need to fake forward to 0001_initial.py where notes was created:

 migrations.CreateModel(
            name='Notes',
            fields=[
                ('price', models.PositiveIntegerField()),
                ('title', models.CharField(max_length=100)),
                ('description', models.CharField(max_length=500)),
                ('thumbnail', models.ImageField(blank=True, null=True, upload_to='notes_thumbnails')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('creator', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notes', to=settings.AUTH_USER_MODEL)),
            ],
        ),

I am so sorry if I am bothering you, but according to everyone on reddit I would need someone who is an expert to fix this snafu.
I am a 17 year old from India, no one I know knows how to code, I taught myself how to do it, so there are gaps in my learning! Thank you for bearing with me

That sounds about right

1 Like

You can use the postgres shell - psql - through Django’s manage.py dbshell command to inspect the state of your tables. I think it’s \d that can describe a table, e.g. \d notes_notes

1 Like

damn, this was useful!

I found out what’s missing! everything from 0005_payment_accounts has not been executed!
so this is what is in 0005:

class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('notes', '0004_auto_20220112_1519'),
    ]

    operations = [
        migrations.CreateModel(
            name='payment_accounts',
            fields=[
                ('cut', models.PositiveSmallIntegerField()),
                ('razorpay_account_id', models.CharField(max_length=50)),
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('account_holder', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='payment_account', to=settings.AUTH_USER_MODEL)),
            ],
        ),
    ]

I think this is true because:

\d notes_payment_accounts
Did not find any relation named "notes_payment_accounts".

so, I think I need to go, I am not sure about 0004 or 0005

python manage.py migrate notes 0004 --fake
python manage.py makemigrations notes
python manage.py migrate notes