Is it good database design?

I have this task to create api:

  1. City district - name, id
  2. Category - name, id
  3. Organization Network - name, id
  4. Organization
  • Belongs to one of the organization networks
  • id, name, description
  • belongs to several districts, can be represented in several of them at the same time
  • have list of products with prices
  1. Product
  • id, name, category
  • can be sold in one or several organizations in network
  • price can be different depending on the organization

This is my try to design this database, am I missing something?

My try to design database

And also in Django:

from django.db import models


class District(models.Model):
    name = models.CharField(max_length=100, unique=True)


class Category(models.Model):
    name = models.CharField(max_length=100, unique=True)


class OrganizationNetwork(models.Model):
    name = models.CharField(max_length=100, unique=True)


class Organization(models.Model):
    name = models.CharField(max_length=100, unique=True)
    description = models.TextField()
    organization_network = models.ForeignKey(
        OrganizationNetwork,
        on_delete=models.CASCADE,
        related_name="organizations",
    )
    district = models.ManyToManyField(
        "District",
        through="DistrictOrganization",
        through_fields=("organization", "district"),
        related_name="organizations",
    )
    products = models.ManyToManyField(
        "Product",
        through="OrganizationProduct",
        through_fields=("organization", "product"),
        related_name="organizations",
    )


class Product(models.Model):
    name = models.CharField(max_length=100, unique=True)
    category = models.ForeignKey(
        Category, on_delete=models.CASCADE, related_name="products"
    )


class DistrictOrganization(models.Model):
    district = models.ForeignKey(District, on_delete=models.CASCADE)
    organization = models.ForeignKey(Organization, on_delete=models.CASCADE)


class OrganizationProduct(models.Model):
    price = models.DecimalField(max_digits=10, decimal_places=2)
    organization = models.ForeignKey(Organization, on_delete=models.CASCADE)
    product = models.ForeignKey(Product, on_delete=models.CASCADE)

It`s pretty strange to create 2 relations between organization and district. You should use one to many if there could be only one organization in district, many to many otherwise.

Back to Top