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

Better documentation for multiple databases #581

Closed
jlauu opened this issue Aug 19, 2019 · 3 comments
Closed

Better documentation for multiple databases #581

jlauu opened this issue Aug 19, 2019 · 3 comments

Comments

@jlauu
Copy link

jlauu commented Aug 19, 2019

Problem Statement
I'm having trouble integrating with my particular use case:

DATABASES = {
  'default': {...}, // this is the Django auto-generated DB where I want historical records stored
  'external_app': {...} // my model instances live in this external DB
} 

My users are also maintained in the default DB.

Describe the solution you'd like
I want historical records to be written to the default app_data Django DB (where admin models are also stored).

Describe alternatives you've considered
Having setup my settings.py accordingly, let MyModel be the model instance of the external db, I've tried:

  1. Simple integration
// This tries to write to `external_app` DB
class MyModel(models.Model):
     history = HistoricalRecords()
  1. Use different app param
// This does not migrate/auto-generate in `default` DB
class MyModel(models.Model):
     history = HistoricalRecords(app="MyApp") // which routes to the `default` DB
  1. Try bases
// This throws a conflict on `id` field of `MyModel` and `MyHistoricalRecord`
class MyHistoricalRecord:
  class Meta:
     app_label = 'default'
     managed = True

class MyModel(models.Model):
     history = HistoricalRecords(app="MyApp", bases=[MyHistoricalRecord]) // which routes to the `default` DB

Seems like 2) is the closest to my desired solution -- however the tables don't exist and when I try to run makemigrations no changes are made.

@rossmechanic
Copy link
Collaborator

What's the error that you get with option 2? Assuming it's a foreign key error on history user?

@jlauu
Copy link
Author

jlauu commented Aug 19, 2019

@rossmechanic
Option 2 throws Programmingerror: (1146, "Table '<default_db_name>.<table>' doesn't exist")

Option 3 throws the conflict

django.core.exceptions.FieldError: Local field 'id' in class 'MyClass' clashes with field of the same name from base class 'MyHistoricalRecords'.

@erikvw
Copy link
Contributor

erikvw commented Feb 29, 2020

@jlauu, probably better to work with UUID primary keys. Try using models.UUIDField for your primary key field (id) on models.

class MyModel(models.Model):
     """Model uses a UUID as a primary key"""
    id = models.UUIDField(default=uuid.uuid4))
    history = HistoricalRecords()

Watch out for use_base_model_db which defaults to False. See the docs.

You may also want the history_id on the historical model to be a UUIDField as well. Do this by setting SIMPLE_HISTORY_HISTORY_ID_USE_UUID = True

@jlauu jlauu closed this as completed Apr 12, 2020
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

No branches or pull requests

3 participants