Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two primary keys specified in MySQL database

I am trying to create a test database that is a replica of a preexisting database. I am using Django models (theoretically the models used with the original database) in order to do this. I recently inherited the code from someone else and am trying to figure out what exactly is going on with the code.

In the model, one the of the tables has two columns identified as the primary key.

        column1 = models.IntegerField(primary_key = True)
        column2 = models.IntegerField(primary_key = True)
        column3 = models.CharField(max_length = 30)

When I try to sync this model to the test database - an error occurs :

File "/somePathHere/MySQLdb/connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (1068, 'Multiple primary key defined')

It is written in the django docs that Django does not allow multiple primary keys. However, looking at the output of DESCRIBE [tablename] in the original MySQL database, it seems like that is exactly what is going on here :

        +------------+------------+------+-----+---------+-------+
        | Field      | Type       | Null | Key | Default | Extra |
        +------------+------------+------+-----+---------+-------+
        | IDENTIFIER | bigint(20) | NO   | PRI | NULL    |       |
        | TIMESTAMP_ | bigint(20) | NO   | PRI | NULL    |       |
        | VALUE_     | longtext   | YES  |     | NULL    |       |
        +------------+------------+------+-----+---------+-------+

Note that both IDENTIFIER and TIMESTAMP_ are listed as the primary keys.

I'm seeing a lot of topics on SO ( Example 1 , Example 2, and Example 3 ) about creating a primary key based on multiple columns - is what I am seeing a composite key? In which case, how is this relayed via a Django model, i.e. how would one replicate it?

If it is not a composite key, what is it?

like image 760
mshell_lauren Avatar asked Jul 19 '11 22:07

mshell_lauren


People also ask

Can we have 2 primary keys in a table in MySQL?

Primary keys must contain UNIQUE values, and cannot contain NULL values. A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).

Can there be 2 primary keys in a table?

Each table can only have one primary key. Access can automatically create a primary key field for you when you create a table, or you can specify the fields that you want to use as the primary key. This article explains how and why to use primary keys.

Can you link 2 primary keys?

Save this answer. Show activity on this post. No there is nothing wrong in linking two identical primary keys in different tables but the problem arises when the data type is declared as identity. When you declare some field as identity then the value is auto incremented and is decided by the seed if provided.

What are the 2 characteristics of primary key in SQL?

A primary key's main features are: It must contain a unique value for each row of data. It cannot contain null values. Every row must have a primary key value.


3 Answers

It's not supported in Django, but there's a workaround. On your model specify unique_together and the fields in the Meta section:

class MyClass(models.Model):
    IDENTIFIER = models.IntegerField(blank=False,null=False)
    TIMESTAMP_ = models.IntegerField(blank=False,null=False)
    VALUE_ = models.TextField(blank=True, null=True)

    class Meta:
        unique_together = ('IDENTIFIER', 'TIMESTAMP_')

This will preserve the two-column primary key behavior.

like image 157
scoopseven Avatar answered Sep 27 '22 23:09

scoopseven


Django currently does not support multi-column primary keys though there are patches/forks that extend it to do so (with varying degrees of polish). From the FAQ "Do Django models support multiple column primary keys?":

No. Only single-column primary keys are supported.

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. Single-column primary keys are needed for things such as the admin interface to work; e.g., you need a simple way of being able to specify an object to edit or delete.

What you're seeing is not two primary keys, but rather a two-column primary key. Tables by definition can only have one primary key.

like image 39
Daniel DiPaolo Avatar answered Sep 27 '22 23:09

Daniel DiPaolo


It's a composite primary key. Try executing this:

show create table mytable;

It should show you the definition of the composite key.

This is nothing "unusual" from a mysql perspective.

like image 43
Bohemian Avatar answered Sep 27 '22 21:09

Bohemian