django の
調べた
前提
環境情報は
- OS
sw_vers ---------------------------- ProductName: Mac OS X ProductVersion: 10.11.6 BuildVersion: 15G1108 ----------------------------
- python version
python3 -V ---------------------------- Python 3.5.1 ----------------------------
- Django Version
pip list ---------------------------- Django (1.10) ----------------------------
- DB
psql -V ---------------------------- psql (PostgreSQL) 9.5.2 ----------------------------
自前で シーケンスオブジェクトを 作成し、 シーケンス取得クエリを 発行する
Django 1.7 で、
sql - Trying to get the next value in sequence from a postgres DB, with django - Stack Overflow と、
Django, accessing PostgreSQL sequence - Stack Overflow を
見る
Migration を
書いて create sequence を 作成する。 select nextval で
シーケンスを 採番する。
方式で、
Migration を 書いて create sequence を 作成する。
- 空のマイグレーションファイルを
作成する
./manage.py makemigrations festivals4partypeople --empty ------------------------------------------ Migrations for 'festivals4partypeople': festivals4partypeople/migrations/0046_auto_20161125_0145.py: ------------------------------------------
- マイグレーションファイルに
migrations.RunSQL
でcreate sequence 文 を 書く
0046_auto_20161125_0145.py を
create sequence を
# -*- coding: utf-8 -*- # Generated by Django 1.10.2 on 2016-11-24 16:45 from __future__ import unicode_literals from django.db import migrations class Migration(migrations.Migration): dependencies = [ ('festivals4partypeople', '0045_auto_20161124_2238'), ] operations = [ "CREATE SEQUENCE festival_classification_seq ", ]
- migrate 実行
./manage.py migrate ----------------------------------- Operations to perform: Apply all migrations: admin, auth, contenttypes, festivals4partypeople, sessions Running migrations: Applying festivals4partypeople.0046_auto_20161125_0145... OK -----------------------------------
- シーケンス取得メソッド作成
@staticmethod def __create_festival_classification_seq(): from django.db import connection cursor = connection.cursor() cursor.execute("select nextval('festival_classification_seq')") result = cursor.fetchone() return result[0]
という
django-sequences を 使って シーケンスを 発行する
aaugustin/django-sequences: Generate gap-less sequences of integer values.
と
dbシーケンスを
simple な シーケンス発行Tableを
インストール
django-sequences/README.rst at master · aaugustin/django-sequences
に
pip install django-sequences ------------------------------------------- Collecting django-sequences Downloading django_sequences-1.0.1-py2.py3-none-any.whl Installing collected packages: django-sequences Successfully installed django-sequences-1.0.1 -------------------------------------------
settings.py に django-sequences を 追加する
INSTALLED_APPS += ['sequences.apps.SequencesConfig']
migrate 実行
python3 manage.py migrate ------------------------------------------ TypeError: can only concatenate tuple (not "list") to tuple ------------------------------------------
INSTALLED_APPS が
INSTALLED_APPS = ( 'festivals4partypeople', 'festivals_crawler.apps.FestivalsCrawlerConfig', 'django_extensions', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'sequences.apps.SequencesConfig' )
再度、
python3 manage.py migrate ------------------------------------ Operations to perform: Apply all migrations: admin, auth, contenttypes, festivals4partypeople, sequences, sessions Running migrations: Applying sequences.0001_initial... OK ------------------------------------
シーケンス採番
以下のように
python 記述
from sequences import get_next_value print(get_next_value('festival_classification_seq'))
output ※2回採番しています。
2
採番テーブル
postgress では、
SQL
select * from sequences_sequence;
output
name | last -----------------------------+------ festival_classification_seq | 2 (1 row)
どちらが いいのか
高負荷時は
別に
今は、
個人的には、
方式でいこうかなと
以上です。
コメント