Skip to content
Advertisement

Django Azure SQL ProgrammingError Invalid Object name

I am trying to use Azure SQL with Django by using mssql-django. I get this error during “makemigrations”:

django.db.utils.ProgrammingError: (’42S02′, “[42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid object name ‘customer_group_customergroup’. (208) (SQLExecDirectW); [42S02] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Statement(s) could not be prepared. (8180)”)

The connection to Azure SQL is already working. If I start a new app, I could make the migrations and migrate it into Azure SQL without problem.

If I migrate only the “customer_group” App, there is no problem. The problem only occurs when I want to migrate apps, whose model contains a foreign key field named customer_group. I tried renaming the field (suspecting it was some naming conflict) but the error still persists.

Does anyone know how to correct this error? Thank you!

Edit: My customer_group app has this model:

class CustomerGroup(models.Model):
    name = models.CharField(max_length=50, unique=True, blank=False, null=False,
                            verbose_name="Customer group name")
    owner_first_name = models.CharField(max_length=20, blank=False, null=False)
    owner_last_name = models.CharField(max_length=20, blank=False, null=False)
    address = models.CharField(max_length=100, blank=False, null=False)
    phone = models.CharField(max_length=15, blank=False, null=False)
    billing_info = models.CharField(max_length=200, blank=False, null=False)
    uuid = models.UUIDField(unique=True, default=uuid.uuid4,
                            editable=False, blank=False, null=False)

    def __str__(self):
        return self.name

Advertisement

Answer

The issue was apparently caused by a view that get some default values from the database. The problem is there are no values yet as the database is empty.

The problem was solved by first commenting the urls so that django does not check the views during migration. Then migrate the database and run the server. I then added the default values that django looks for and uncomment the urls.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement