Skip to content

Commit

Permalink
chore: clean up some warnings and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
olavloite committed Feb 18, 2023
1 parent 5ea9615 commit 05e2357
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 20 deletions.
2 changes: 1 addition & 1 deletion samples/python/django/create_data_model.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion samples/python/django/drop_data_model.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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;
drop table if exists singers;
17 changes: 16 additions & 1 deletion samples/python/django/sample.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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')
Expand Down
41 changes: 24 additions & 17 deletions samples/python/django/sample_app/model.py
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -63,17 +68,19 @@ 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)
description = models.JSONField()


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)
Expand Down

0 comments on commit 05e2357

Please sign in to comment.