Search code examples
djangomany-to-many

Django - "ManyToMany" without primary key (AutoField)


Is it even possible to create a model (or a many to many relation) in Django without id field (AutoField) in database?

For example, I have models: Task and User. User can have assigned many Tasks and Task can be assigned to many Users. Generally, Django will create the relationship table with fields like id, user_id, task_id. Can a id field be omitted? The user_id and task_id fields will be marked as unique_together.


Solution

  • No, it is not possible to create a many to many field with just the user_id and task_id fields.

    All Django models must have exactly one primary key field. It is not yet possible to use composite primary keys (e.g. (user_id, task_id)).