Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create django model without primary key column and avoiding Id default column

I am using SQL Server as a default database. I replicated the SQL Server table in the model using inspectdb command. Now, SQL Server table does not have any primary key column. So, django model automatically creates id as a primary key and giving me error while posting the data.

column "id" does not exist

Is there any work around to remove id at the same time not defining primary key on any columns of the model?

from django.db import models

class ImptCustomer(models.Model):
    customerid = models.IntegerField(db_column='CustomerId', blank=True, null=True)  # Field name made lowercase.
    name = models.CharField(db_column='Name', max_length=50, blank=True, null=True)  # Field name made lowercase.
    phonenumber = models.CharField(db_column='PhoneNumber', max_length=20, blank=True, null=True)  # Field name made lowercase.
    emailaddress = models.CharField(db_column='EmailAddress', max_length=100, blank=True, null=True)  # Field name made lowercase.
    streetline = models.CharField(db_column='StreetLine', max_length=50, blank=True, null=True)  # Field name made lowercase.
    city = models.CharField(db_column='City', max_length=50, blank=True, null=True)  # Field name made lowercase.
    statecode = models.CharField(db_column='StateCode', max_length=5, blank=True, null=True)  # Field name made lowercase.
    postalcode = models.CharField(db_column='PostalCode', max_length=20, blank=True, null=True)  # Field name made lowercase.
    customer_key = models.IntegerField(db_column='Customer_Key', blank=True, null=True, editable = False)  # Field name made lowercase.

    class Meta:
        managed = False
        db_table = 'IMPT_Customer'
like image 431
Jay Desai Avatar asked Oct 16 '22 05:10

Jay Desai


1 Answers

If you don't explicitly define a primary key, then the id becomes your primary key. So you cannot do that.

like image 141
Daedalus Avatar answered Oct 20 '22 10:10

Daedalus