Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: extract base model from member service #532

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

braaar
Copy link
Collaborator

@braaar braaar commented Feb 1, 2025

Here I am trying to extract the most general fields of a service, stripping away anything that is lab-specific or assumes a certain kind of way of doing things.

see #531 for the reasoning behind this.

I have come across a problem with this approach:

It is impossible to add a non-nullable field 'baseservice_ptr' to memberservice without specifying a default. This is because the database needs something to populate existing rows.
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Quit and manually define a default value in models.py.

It seems we can't retroactively tell Django that a model wants to become a child of another model due to a (presumably) required foreign to the other model.

@braaar
Copy link
Collaborator Author

braaar commented Feb 1, 2025

We might need to some som manual migration trickery like in this stackoverflow thread (expect we want to do the opposite)

@drjaska
Copy link
Collaborator

drjaska commented Feb 1, 2025

Code changes look good assuming that the new extra fields are meaningless but this likely needs database migrations? @tswfi

Copy link
Member

@vranki vranki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Good idea.

@tswfi
Copy link
Member

tswfi commented Feb 1, 2025

Code changes look good assuming that the new extra fields are meaningless but this likely needs database migrations? @tswfi

yup, this will probably require a manual migration file to be written. Django is usually pretty good at generating the migrations automatically but that seems to not be the case here..

Also we might want consider using a global base model that always adds things like CreationDateTime LastUpdateDateTime and so on for all models that we have.

The base model should also be marked as Abstract https://docs.djangoproject.com/en/5.1/topics/db/models/#abstract-base-classes as it should probably never be used directly.

@tswfi tswfi marked this pull request as draft February 1, 2025 18:19
@tswfi
Copy link
Member

tswfi commented Feb 1, 2025

I converted this to draft so it is not merged before at least the migration is ok

@braaar
Copy link
Collaborator Author

braaar commented Feb 3, 2025

@tswfi's suggestion about making the class abstract solved the migration problem! I ended up removing the service_id field since it is probably superfluous given that there already is an ID field under the hood (in the base model, I reckon). Having a special unique identifier on top of that feels too use case specific to include in the base model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants