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

Configure second GIS database and router, add example model and skeleton models for remainder of quercus schema #70

Merged
merged 7 commits into from
Dec 3, 2021

Conversation

hancush
Copy link
Collaborator

@hancush hancush commented Dec 2, 2021

Overview

See title. It also adds a test for the database routing.

Closes #60

Testing Instructions

  • Run the app and load the fixture data and GIS data dump, as documented in the README.
  • Open up a Django shell.
  • Query for a managed model (e.g., Section) and confirm you get hits: Section.objects.all()
  • Query for a NaturePreserves and confirm you get hits: NaturePreserves.objects.all()

@smcalilly smcalilly temporarily deployed to ccfp-asset-d-gis-models-mnfxgr December 2, 2021 21:02 Inactive

class Meta:
managed = False
db_table = '"quercus"."nature_preserves"'
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Needed to quote the schema and table names for this to work. Not sure why, also lost StackOverflow reference, sorry!

managed = False
db_table = '"quercus"."nature_preserves"'

id = models.AutoField(primary_key=True, db_column='nature_preserves_id')
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

When a primary key isn't explicitly specified, Django tries to use id. There isn't an id column in many of these tables, so you have to specify it in db_column.

@@ -0,0 +1,49 @@
class GISRouter:
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This tells Django which DB to route ORM queries to. See: https://docs.djangoproject.com/en/3.2/topics/db/multi-db/#automatic-database-routing

@hancush hancush temporarily deployed to ccfp-asset-d-gis-models-mnfxgr December 3, 2021 14:11 Inactive
@hancush hancush temporarily deployed to ccfp-asset-d-gis-models-mnfxgr December 3, 2021 14:19 Inactive
@hancush hancush temporarily deployed to ccfp-asset-d-gis-models-mnfxgr December 3, 2021 18:43 Inactive
return self.site_name


class Buildings(GISModel):
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@smcalilly roughed in models for the remaining tables. The db_table attribute needs to be updated to quote the schema and table names, and the columns we need to use need to be defined as attributes.


@pytest.mark.django_db(databases=['default', 'fp_postgis'])
def test_gis_query(nature_preserves):
assert NaturePreserves.objects.db == 'fp_postgis'
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If using is not passed, this attribute is set by the db_for_read() method of the router. See: https://github.com/django/django/blob/ca9872905559026af82000e46cde6f7dedc897b6/django/db/models/manager.py#L135

@hancush hancush requested a review from fgregg December 3, 2021 18:52
Copy link
Collaborator

@fgregg fgregg left a comment

Choose a reason for hiding this comment

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

looks really good, @hancush !

@hancush hancush merged commit 642ca02 into master Dec 3, 2021

class Buildings(GISModel):

class Meta(GISModel.Meta):
Copy link
Contributor

Choose a reason for hiding this comment

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

oh cool, i was wondering how to inherit so i didn't have to add managed = False to every model.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah! The class-on-class Meta inheritance is a little wonky, but it's buried down there in the docs. (I had to Google it 😅)

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.

How do we want to connect to the FPDCC’s GIS Database
3 participants