From 05e2357f4c50b0d4f2cee4337afa67b09e6dcd2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Knut=20Olav=20L=C3=B8ite?= Date: Sat, 18 Feb 2023 12:59:33 +0100 Subject: [PATCH] chore: clean up some warnings and comments --- samples/python/django/create_data_model.sql | 2 +- samples/python/django/drop_data_model.sql | 2 +- samples/python/django/sample.py | 17 ++++++++- samples/python/django/sample_app/model.py | 41 ++++++++++++--------- 4 files changed, 42 insertions(+), 20 deletions(-) diff --git a/samples/python/django/create_data_model.sql b/samples/python/django/create_data_model.sql index 9bf5ea371..dea7d2541 100644 --- a/samples/python/django/create_data_model.sql +++ b/samples/python/django/create_data_model.sql @@ -33,7 +33,7 @@ CREATE TABLE IF NOT EXISTS tracks ( PRIMARY KEY(id, track_number) ) INTERLEAVE IN PARENT albums ON DELETE CASCADE; -CREATE UNIQUE INDEX IF NOT EXISTS unique_idx_id ON tracks(track_id); +CREATE UNIQUE INDEX IF NOT EXISTS idx_tracks_track_id ON tracks(track_id); CREATE TABLE IF NOT EXISTS venues ( id character varying NOT NULL, diff --git a/samples/python/django/drop_data_model.sql b/samples/python/django/drop_data_model.sql index f056cc9e3..23e7b65d3 100644 --- a/samples/python/django/drop_data_model.sql +++ b/samples/python/django/drop_data_model.sql @@ -2,4 +2,4 @@ drop table if exists concerts; drop table if exists venues; drop table if exists tracks; drop table if exists albums; -drop table if exists singers; \ No newline at end of file +drop table if exists singers; diff --git a/samples/python/django/sample.py b/samples/python/django/sample.py index b8ac90085..caf640c6c 100644 --- a/samples/python/django/sample.py +++ b/samples/python/django/sample.py @@ -1,3 +1,18 @@ +""" Copyright 2022 Google LLC + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +""" + import os import sys import datetime @@ -300,8 +315,8 @@ def jsonb_filter(): if __name__ == "__main__": + tables_created: bool = False try: - tables_created = False # setting up django os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'setting') diff --git a/samples/python/django/sample_app/model.py b/samples/python/django/sample_app/model.py index f4c88032d..400455c59 100644 --- a/samples/python/django/sample_app/model.py +++ b/samples/python/django/sample_app/model.py @@ -1,4 +1,4 @@ -''' Copyright 2022 Google LLC +""" Copyright 2022 Google LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -11,20 +11,19 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. -''' +""" from django.db import models from django.db.models import Q, F -from django.contrib.postgres.fields import JSONField class BaseModel(models.Model): - class Meta(): + class Meta: abstract = True created_at = models.DateTimeField() updated_at = models.DateTimeField() class Singer(BaseModel): - class Meta(): + class Meta: db_table = 'singers' id = models.CharField(primary_key=True, null=False) @@ -34,7 +33,7 @@ class Meta(): active = models.BooleanField() class Album(BaseModel): - class Meta(): + class Meta: db_table = 'albums' id = models.CharField(primary_key=True, null=False) @@ -45,16 +44,22 @@ class Meta(): singer = models.ForeignKey(Singer, on_delete=models.DO_NOTHING) class Track(BaseModel): - class Meta(): + class Meta: db_table = 'tracks' - # Here, track_id is a column that is supposed to be primary key by Django. - # But id column will just have a unique index in the actual table. - # In the actual table, (id, track_number) will be the primary key. - # This is done because Django doesn't support composite primary keys, - # but we need to have a composite primary key due to the fact that - # the "tracks" table is interleaved in "albums". - + # Track is interleaved in the parent table Album. Cloud Spanner requires that + # a child table includes all the primary key columns of the parent table in + # its primary key, followed by any primary key column(s) of the child table. + # This means that a child table will always have a composite primary key. + # Composite primary keys are however not supported by Django. The workaround + # that we apply here is to create a separate field `track_id` and tell Django + # that this is the primary key of the table. The actual schema definition + # for the `tracks` table does not have this as its primary key. Instead, the + # primary key of this table is (`id`, `track_number`). In addition, there is a + # unique index defined on `track_id` to ensure that Track rows can efficiently + # be retrieved using the identifier that Django thinks is the primary key of + # this table. + # See create_data_model.sql file in this directory for the table definition. track_id = models.CharField(primary_key=True, null=False) album = models.ForeignKey(Album, on_delete=models.DO_NOTHING, db_column='id') track_number = models.BigIntegerField(null=False) @@ -63,7 +68,7 @@ class Meta(): class Venue(BaseModel): - class Meta(): + class Meta: db_table = 'venues' id = models.CharField(primary_key=True, null=False) name = models.CharField(null=False) @@ -71,9 +76,11 @@ class Meta(): class Concert(BaseModel): - class Meta(): + class Meta: db_table = 'concerts' - constraints = [models.CheckConstraint(check = Q(end_time__gte=F('start_time')), name='chk_end_time_after_start_time' )] + constraints = [models.CheckConstraint( + check = Q(end_time__gte=F('start_time')), + name='chk_end_time_after_start_time' )] id = models.CharField(primary_key=True, null=False) venue = models.ForeignKey(Venue, on_delete=models.DO_NOTHING) singer = models.ForeignKey(Singer, on_delete=models.DO_NOTHING)