greenelab / adage-backend

The backend for Adage web app
https://adage.greenelab.com
BSD 3-Clause "New" or "Revised" License
2 stars 2 forks source link

Use auto ID as primary key in "Experiment" model #47

Closed dongbohu closed 4 years ago

dongbohu commented 4 years ago

Right now the primary key in Experiment model is acecession field:

# analyze/models.py:
class Experiment(models.Model):
    accession = models.CharField(max_length=48, primary_key=True)
    ...

The frontend prefers using an auto-incremented id field as the primary key (like most of the other models). This can be done by changing the line above into:

    accession = models.CharField(max_length=48, unique=True)

But this will affect analyze_experiment and ananlyze_sample_experiments tables, both of which are populated by a management command. When both tables already have records, the migration is very awkward because the new id field is not only the primary key in analyze_experiment table but also the foreign key in analyze_sample_experiments table.

The best solution is to:

  1. Delete all migration files in analyze/migration/;
  2. Create a brandnew database: psql> create database <new_db_name> or shell> createdb <new_db_name>;
  3. Run manage.py makemigration and manage.py migrate to initialize all tables;
  4. Run all management commands to rebuild all tables from scratch;
  5. Change Django config so that the Django project points to the new database.
dongbohu commented 4 years ago

The new id field should be added into Experiment's serializer class too: https://github.com/greenelab/py3-adage-backend/blob/74de24d82631d7e176b61fff367dd9ad6024e067/adage/analyze/serializers.py#L21

dongbohu commented 4 years ago

Fixed in PR #55.