title: makemigrations results in django.db.utils.OperationalError: no such column: (Python 3.10 and Django 4.0.3)
body: <p>First, and this is really important, this error is being returned WHEN I run <code>python manage.py makemigrations</code> (Python 3.10 and Django 4.0.3)</p>
  <p>In my research of the many instances of this error I've seen that as the first suggestion for this particular error message. Unfortunately this issue is NOT going to be solved with just running <code>python manage.py makemigrations</code>.</p>
  <p>The error I get when adding a new field and running makemigrations is:</p>
  <p><code>django.db.utils.OperationalError: no such column: events_eventsetting.timezone_aware_venues</code></p>
  <p>OK, now for some additional details. This is not my first Django project. I've been building some solid apps and am very comfortable with adding fields to models. Many, many times, I've done the following without a hitch...until a few days ago</p>
  <ul>
  <li>Add a new syntactically correct field</li>
  <li>run <code>python manage.py makemigrations</code></li>
  <li>run <code>python manage.py migrate</code></li>
  </ul>
  <p>And that's being going very well until a couple of days ago. For additional context, I have not changed the virtual environment in anyway - the requirements.txt file has the exact same contents. I have also added no apps or made any changes to settings.py. Also, I have made no references to the field in any other location. The addition of this field, in both instances, to the model was the very first reference made to the field.</p>
  <p>In other words, I get that until a field has been migrated into the DB there is no point in referencing it. And besides, PyCharm will start making lots of noise when you try to do that anyway.</p>
  <p>And so, after some looking around, I came away puzzled. Why would an operation designed to prepare the SQL to migrate the DB tell me there's no such column? My first round of research yielded no solution.</p>
  <p>After much digging into this I basically decided to completely rebuild the project. Not being a SQL expert, and failing to resolve this by deleting all migration files and the db.sqlite3 file I simply rebuilt the project from the ground up with lots of copy/pasting (in a fresh folder and a new git repository).</p>
  <p>Fast forward to one day later. Adding a new field breaks an already working project...AGAIN! The project works great, the DB is connected, I can use every app in the project, and validate through various tests that all is well.</p>
  <p>Now for some project specifics. <strong>Here is the model in question from the models.py file.</strong></p>
  <pre><code>from django.utils import timezone
  from django.db import models
  from django.contrib.auth.models import User
  
  
  YES_NO_CHOICES = [
      ('No', 'No'),
      ('Yes', 'Yes'),
  ]
  
  
  class EventSetting(models.Model):
      company_signature = models.CharField(
          max_length=80,
          null=True,
          blank=True,
          help_text='''Please enter the name and tagline for the company used by Whurthy for signatures.'''
      )
      event_payment_gateway = models.CharField(
          max_length=10,
          choices=EVENT_PAYMENT_CHOICES,
          default='Manual',
          help_text='''Select &quot;Manual&quot; to process payments manually through Event Attendance. You should only 
          select &quot;Stripe&quot; if this has been setup as the payment gateway for this installation. If selected, ach 
          event will have the option of manual payment or Stripe payment.'''
      )
      allow_refunds_after_attendance = models.CharField(
          max_length=5,
          choices=YES_NO_CHOICES,
          default='Yes',
          help_text='''Do you wish to allow refunds after an event booking has been marked as &quot;Attended&quot;? In short, 
          selecting &quot;Yes&quot; allows for a refund to be processed through Whurthy on an already completed and attended event. 
          Selecting &quot;No&quot; will mean that any refunding will have to be processed through the, more complicated, admin panel
          . Note: This is a beta feature designed to work fully when the event payment gateway is set to &quot;Manual&quot;.
          '''
      )
      multiple_venues = models.CharField(
          max_length=5,
          choices=YES_NO_CHOICES,
          default='Yes',
          help_text='''Does this organization have multiple venues/rooms for events? Selecting No will cause Whurthy to 
          use the default Business Address. Selecting Yes will require you to select a venue for each event
          .'''
      )
      single_location_address = models.TextField(
          null=True,
          blank=True,
          help_text='''If Whurthy is being used for events at just one venue/room enter an address. This address will only
           be used if [Multiple Venues] was selected as No. This setting is only recommended for organizations with only 1
           room or venue.'''
      )
      display_seating_to_customers = models.CharField(
          max_length=5,
          choices=YES_NO_CHOICES,
          default='Yes',
          help_text='''Do you wish to display available event seating to Customers? Selecting Yes will display seat 
          availability to customers. Selecting No will hide seating availability. This will not have any impact on waiting
           list calculations.'''
      )
      automated_loyalty_program = models.CharField(
          max_length=5,
          choices=YES_NO_CHOICES,
          default='Yes',
          help_text='''Do you want to activate the automated loyalty program for customers? If you select Yes, then 
          additional options will appear allowing you to set the thresholds at which customers are elevated to the next 
          loyalty tier. Selecting &quot;No&quot; will mean that the only way to change a customer's loyalty tier will be through 
          the admin panel. '''
      )
      member_loyalty_threshold = models.IntegerField(
          null=True,
          blank=True,
          default=10,
          validators=[MinValueValidator(0), MaxValueValidator(1000)],
          help_text='''Enter the threshold for the member loyalty tier. For example, if you enter 10 then a new customer         
          will automatically be elevated to the Member loyalty tier after 10 &quot;completed&quot; bookings. Note: all newly created 
           accounts have the loyalty tier of &quot;Guest&quot; unless changed manually.'''
      )
      VIP_loyalty_threshold = models.IntegerField(
          null=True,
          blank=True,
          default=50,
          validators=[MinValueValidator(0), MaxValueValidator(1000)],
          help_text='''Enter the threshold for the VIP loyalty tier. For example, if you enter 50 then a &quot;Member&quot; customer 
           will automatically be elevated to the VIP loyalty tier after 50 completed bookings. Whurthy will add the member
            threshold and the VIP threshold to place a customer in the VIP tier.'''
      )
      tax_events = models.CharField(
          max_length=5,
          choices=YES_NO_CHOICES,
          default='No',
          help_text='''Do you wish to explicitly add tax to events? Selecting &quot;No&quot; will result in the event amounts being
           net. Selecting &quot;Yes&quot; will allow you to explicitly set tax labels and amounts to be applied to events.'''
      )
      tax_scope = models.CharField(
          max_length=10,
          choices=TAX_SCOPE_CHOICES,
          default='Global',
          help_text='''Do you wish to set a single tax structure for all events, or to set a tax structure for each venue?
           Selecting &quot;By Venue&quot; will require you to enter at least one tax label and amount for each venue.'''
      )
      tax_1_label = models.CharField(
          null=True,
          blank=True,
          max_length=18,
          help_text='''Enter a tax label relevant to your tax jurisdiction. For example, in Canada you could enter GST for
           Goods and Services Tax, or in the UK you could enter VAT for Value Added Tax.'''
      )
      tax_1_amount = models.DecimalField(
          null=True,
          blank=True,
          default=0.0,
          max_digits=6,
          decimal_places=2,
          validators=[MinValueValidator(0), MaxValueValidator(100)],
          help_text='''Enter the tax percentage for the &quot;Tax 1 label&quot;'''
      )
      tax_2_label = models.CharField(
          null=True,
          blank=True,
          max_length=18,
          help_text='''Enter a (second and optional) tax label relevant to your tax jurisdiction.'''
      )
      tax_2_amount = models.DecimalField(
          null=True,
          blank=True,
          default=0.0,
          max_digits=6,
          decimal_places=2,
          validators=[MinValueValidator(0), MaxValueValidator(100)],
          help_text='''Enter the tax percentage for the &quot;Tax 2 label&quot;'''
      )
      use_twilio = models.CharField(
          max_length=5,
          choices=YES_NO_CHOICES,
          default='No',
          help_text='''Will Twilio be used to send SMS/Text notifications?'''
      )
      twilio_account_id = models.CharField(
          null=True,
          blank=True,
          max_length=80,
          help_text='''Enter the Twilio Account ID.'''
      )
      twilio_phone = models.CharField(
          null=True,
          blank=True,
          max_length=31,
          help_text='''Enter the Twilio phone number.'''
      )
      twilio_messaging_id = models.CharField(
          null=True,
          blank=True,
          max_length=40,
          help_text='''Enter the Twilio messaging ID.'''
      )
  </code></pre>
  <p>The above models file was built over the course of two migrations. When rebuilding the project I copy pasted in the model from the old project, with the new field I had been trying to add that Django reported as there being no column for. I then made another change by altering some fields.</p>
  <p>Please note that until this point, the rebuild had gone perfectly. The only changes I had made were to a view and a template.</p>
  <p>However, when I went to <strong>add the following to models.py:</strong></p>
  <pre><code>    timezone_aware_venues = models.CharField(
          choices=YES_NO_CHOICES,
          max_length=5,
          default='Yes',
      )
  </code></pre>
  <p>I got the same error.</p>
  <p><strong>What other troubleshooting have I tried?</strong></p>
  <p>I have tried the additional steps. I can successfully create a completely new model, copy/pasting the text from the existing model with the new field I am trying add. makemigrations runs perfectly, returning the note that a new model has been created.</p>
  <p>I have deleted all <code>__pycache__</code> files.</p>
  <p>I have manually built the migrations file:</p>
  <pre><code>pyfrom django.db import migrations, models
  
  class Migration(migrations.Migration):
  
      dependencies = [
          ('events', '0002_alter_event_event_cost_currency_and_more'),
      ]
  
      operations = [
          migrations.AddField(
              model_name='event',
              name='timezone_aware_venues',
              field=models.CharField(blank=True, choices=[('Yes', 'Yes'), ('No', 'No')], default='Yes', max_length=10, null=True),
          ),
      ]
  </code></pre>
  <p>The above yields EXACTLY the same error.</p>
  <p>I have replicated this on two different computers. I am using PyCharm.</p>
  <p>What breaks my brain on this is that makemigrations is built to create the migration file for SQL to be built.</p>
  <p>When bypassing makemigrations and manually writing my own migration file, the idea that the above instruction to <code>AddField</code> returns an error telling me that there is no such field really highlights that I am unaware of the cause of this issue.</p>
  <p>This is all on a new project built just a couple of days ago. I can prove syntax is not the cause. The code is correct, and I can prove this because I can copy/paste the entire model with the error-invoking field for a completely new model and NOT get this error.</p>
  <p><strong>If there is a DB connection issue, I am mystified. The project works great on the front end. If there is a coding issue, I am mystified. I can build a brand new model with a new name, use all the existing fields and the new field I'm trying to add and makemigrations runs perfectly!</strong></p>
  <p>I can workaround this by rebuilding this project, again, from the ground up. I did this two days ago, on a completely new git repository, in a completely fresh folder, with a completely new venv.</p>
  <p>HOWEVER, this is a workaround. It is not cause. It's my least favourite option. I don't know what is causing this issue, and can only assume this could happen again. After all, this is the second project for which it has happened in the past few days.</p>
  <p>That said, if I can't get past this I have no choice. I'll simply have to rebuild the project from the ground up, again, add fields, and away I go. I tried to delete all migration files and the db.sqlite3 file the last time, run makemigrations to rebuild the DB and got the same error again. The quickest solution, hours though it is, is literally start all over again until this error comes up.</p>
  <p>There is something amiss in the Django framework on this. makemigrations is designed for the purpose of writing the file that updates a SQL db. The error appears irrational given the context. And the workaround of having to restart projects from scratch, with lots of copy/pasting to demonstrate my code was correct all along, points to this being something not caused by me. I fully expect to return to this post in a day or two with an edit saying, &quot;Yep, yet again I rebuilt the project from scratch again and that field was added without a hitch when migrating the db.&quot;</p>
  <p>Given the number of unanswered similar issues I'm not expecting a silver bullet answer. But are there any Django and db experts out there that can point me a troubleshooting path that consists of advice more sophisticated than run <code>python manage.py makemigrations</code>?</p>
  <p><strong>EDIT April 7:</strong></p>
  <p>Today, I built a new project from the ground up, and successfully copy/pasted over the very change I had to make. However, this third time I used PostgreSQL. Indeed, the code I'm using to add the column IS correct!</p>
  <p>Everything went great...until, I went to add a new column to the DB again.</p>
  <p>I can (and will) workaround this by rebuilding the DB. However, from what I can tell I'm going to have to build a new DB every time I need to make a change to the model. For some (very strange) reason there is something I've done in this project that is creating a problem that makes no sense.</p>
  <p>Here's the traceback I get. It's eerily similar...</p>
  <pre><code>python manage.py makemigrations
  Traceback (most recent call last):
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\db\backends\utils.py&quot;, line 89, in _execute
      return self.cursor.execute(sql, params)
  psycopg2.errors.UndefinedColumn: column events_eventsetting.sms_messaging does not exist
  LINE 1: ...x_2_label&quot;, &quot;events_eventsetting&quot;.&quot;tax_2_amount&quot;, &quot;events_ev...
                                                               ^
  
  
  The above exception was the direct cause of the following exception:
  
  Traceback (most recent call last):
    File &quot;C:\Users\carmi\Documents\GitHub\Whurthy\manage.py&quot;, line 22, in &lt;module&gt;
      main()
    File &quot;C:\Users\carmi\Documents\GitHub\Whurthy\manage.py&quot;, line 18, in main
      execute_from_command_line(sys.argv)
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\core\management\__init__.py&quot;, line 446, in execute_from_command_line
      utility.execute()
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\core\management\__init__.py&quot;, line 440, in execute
      self.fetch_command(subcommand).run_from_argv(self.argv)
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\core\management\base.py&quot;, line 414, in run_from_argv
      self.execute(*args, **cmd_options)
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\core\management\base.py&quot;, line 455, in execute
      self.check()
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\core\management\base.py&quot;, line 487, in check
      all_issues = checks.run_checks(
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\core\checks\registry.py&quot;, line 88, in run_checks
      new_errors = check(app_configs=app_configs, databases=databases)
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\core\checks\urls.py&quot;, line 14, in check_url_config
      return check_resolver(resolver)
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\core\checks\urls.py&quot;, line 24, in check_resolver
      return check_method()
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\urls\resolvers.py&quot;, line 480, in check
      for pattern in self.url_patterns:
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\utils\functional.py&quot;, line 49, in __get__
      res = instance.__dict__[self.name] = self.func(instance)
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\urls\resolvers.py&quot;, line 696, in url_patterns
      patterns = getattr(self.urlconf_module, &quot;urlpatterns&quot;, self.urlconf_module)
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\utils\functional.py&quot;, line 49, in __get__
      res = instance.__dict__[self.name] = self.func(instance)
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\urls\resolvers.py&quot;, line 689, in urlconf_module
      return import_module(self.urlconf_name)
    File &quot;C:\Users\carmi\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py&quot;, line 126, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1050, in _gcd_import
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1027, in _find_and_load
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1006, in _find_and_load_unlocked
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 688, in _load_unlocked
    File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 883, in exec_module
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 241, in _call_with_frames_removed
    File &quot;C:\Users\carmi\Documents\GitHub\Whurthy\Whurthy\urls.py&quot;, line 51, in &lt;module&gt;
      path('', include('ops.urls')),
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\urls\conf.py&quot;, line 38, in include
      urlconf_module = import_module(urlconf_module)
    File &quot;C:\Users\carmi\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py&quot;, line 126, in import_module
      return _bootstrap._gcd_import(name[level:], package, level)
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1050, in _gcd_import
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1027, in _find_and_load
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 1006, in _find_and_load_unlocked
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 688, in _load_unlocked
    File &quot;&lt;frozen importlib._bootstrap_external&gt;&quot;, line 883, in exec_module
    File &quot;&lt;frozen importlib._bootstrap&gt;&quot;, line 241, in _call_with_frames_removed
    File &quot;C:\Users\carmi\Documents\GitHub\Whurthy\ops\urls.py&quot;, line 3, in &lt;module&gt;
      from .views import (
    File &quot;C:\Users\carmi\Documents\GitHub\Whurthy\ops\views.py&quot;, line 44, in &lt;module&gt;
      COMPANY_EMAIL_SIGNATURE = EventSetting.objects.all().first().company_signature
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\db\models\query.py&quot;, line 753, in first
      for obj in (self if self.ordered else self.order_by(&quot;pk&quot;))[:1]:
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\db\models\query.py&quot;, line 320, in __iter__
      self._fetch_all()
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\db\models\query.py&quot;, line 1507, in _fetch_all
      self._result_cache = list(self._iterable_class(self))
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\db\models\query.py&quot;, line 57, in __iter__
      results = compiler.execute_sql(
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\db\models\sql\compiler.py&quot;, line 1361, in execute_sql
      cursor.execute(sql, params)
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\db\backends\utils.py&quot;, line 103, in execute
      return super().execute(sql, params)
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\db\backends\utils.py&quot;, line 67, in execute
      return self._execute_with_wrappers(
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\db\backends\utils.py&quot;, line 80, in _execute_with_wrappers
      return executor(sql, params, many, context)
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\db\backends\utils.py&quot;, line 84, in _execute
      with self.db.wrap_database_errors:
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\db\utils.py&quot;, line 91, in __exit__
      raise dj_exc_value.with_traceback(traceback) from exc_value
    File &quot;C:\Users\carmi\Documents\GitHub\venv_whurthy\lib\site-packages\django\db\backends\utils.py&quot;, line 89, in _execute
      return self.cursor.execute(sql, params)
  django.db.utils.ProgrammingError: column events_eventsetting.sms_messaging does not exist
  LINE 1: ...x_2_label&quot;, &quot;events_eventsetting&quot;.&quot;tax_2_amount&quot;, &quot;events_ev...
  </code></pre>
  <p><strong>EDIT April 7:</strong></p>
  <p>After posting this issue I went and rebuilt the Django project, basing it on PostgreSQL (replacing sqlite3) and copy/pasting over the models from the original project. As expected, the code worked perfectly on a fresh DB.</p>
  <p>However, when I went to add a new column I got a very similar error. The trackback is at the end of the post.</p>
  <p>My only workaround is to manually rebuild the project if I want to add a column to a particular table in this DB. The fact I'm getting this in both sqlite3 and PostgreSQL tells me there's either something I'm doing that Django doesn't like or there's a bug in the framework (unlikely, but not impossible).</p>
  <p>In the meantime, I'll keep working around this issue, proving that my code each and every time is correct when I rebuild the Django project (with lots of copy/pasting).</p>