Ahmed Yasin
Ahmed Yasin

Reputation: 948

How can we keep unique id, if me make primary key any other column

I want to use default generation ID as well and make another field a primary key.. Can I do that with django ?.

this is my object::

{
    "platform_subscriber_id": "XXXXXXXXXXXXXXX",#this is custom primary key.
    "platform_subscriber_entity": "loop",
    "platform_subscriber_secret_key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
    "is_remove": false,
    "update_time": "2022-02-09T09:18:28.991032Z",
    "create_time": "2022-02-09T09:18:28.991038Z",
    "platform_subscriber_owner": 1
},

i want old one unique ID too..

{
        "id":1,#this is default one i want back
        "platform_subscriber_id": "XXXXXXXXXXXXXXX",#this is custom primary key.
        "platform_subscriber_entity": "loop",
        "platform_subscriber_secret_key": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
        "is_remove": false,
        "update_time": "2022-02-09T09:18:28.991032Z",
        "create_time": "2022-02-09T09:18:28.991038Z",
        "platform_subscriber_owner": 1
    }

this is how my model looks like:

class Project(models.Model):
    platform_subscriber_owner = models.ForeignKey(User, verbose_name=_("project owner"),db_column="platform_user_id", on_delete=models.CASCADE,null=True,blank=True)
    platform_subscriber_id = models.CharField(_("Project Key"), db_column="platform_subscriber_key", max_length=64,primary_key=True, blank=True)
    platform_subscriber_entity = models.CharField(_("Project Names"), max_length=64,blank=True)
    platform_subscriber_secret_key = models.CharField(_("Project Secret Key"), max_length=64,blank=True)
    is_remove = models.BooleanField(_("Remove"),default=False)
    update_time = models.DateTimeField(_("Updated Time"), default = timezone.now)
    create_time = models.DateTimeField(_("Created Time"), default = timezone.now)

Upvotes: 0

Views: 193

Answers (1)

aberkb
aberkb

Reputation: 674

No. Only single-column primary keys are supported. You can make it unique=True instead of primary_key=True would that solve your problem check this

But this isn’t an issue in practice, because there’s nothing stopping you from adding other constraints (using the unique_together model option or creating the constraint directly in your database), and enforcing the uniqueness at that level.

Upvotes: 1

Related Questions