Skip to content

Commit

Permalink
Use django.db.models.Max.aggregate to get nextautoincrement (#1114)
Browse files Browse the repository at this point in the history
The SQL query was not compatible with postgresql
  • Loading branch information
ZephOne authored Apr 19, 2024
1 parent 6966878 commit 43a7ce4
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions pod/main/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from django.template.defaultfilters import slugify
from django.dispatch import receiver
from django.db.models.signals import post_save
from django.db import connection
from django.db.models import Max
import os
import mimetypes
from ckeditor.fields import RichTextField
Expand All @@ -19,16 +19,11 @@


def get_nextautoincrement(model):
cursor = connection.cursor()
cursor.execute(
"SELECT Auto_increment FROM information_schema.tables "
+ 'WHERE table_name="{0}" AND table_schema=DATABASE();'.format(
model._meta.db_table
)
)
row = cursor.fetchone()
cursor.close()
return row[0]
"""Get potential next id for the current model when an object will be created"""
objects = model.objects.all()
if objects:
return objects.aggregate(Max("id"))["id__max"] + 1
return 1


def get_upload_path_files(instance, filename):
Expand Down

0 comments on commit 43a7ce4

Please sign in to comment.