#Making my own Snowflake field

6 messages · Page 1 of 1 (latest)

native schooner
#

So I am starting a school project, and instead/alongside of using django's id, I want to use a Snowflake id (the kind of ID discord/twitter uses). I tried subclassing BigAutoField , here's my code:

from snowflake import SnowflakeGenerator, Snowflake # i can link this if needed

class SnowflakeField(models.BigAutoField):
    __generator: ClassVar[SnowflakeGenerator] = SnowflakeGenerator(1)

    def __init__(self, *args, **kwargs):
        kwargs['blank'] = False
        kwargs['editable'] = False
        kwargs['unique'] = True
        kwargs['primary_key'] = True
        kwargs['default'] = SnowflakeField.__generator.__next__()

        super().__init__(*args, **kwargs)

    def db_type(self, connection):
        return 'bigint'

    def pre_save(self, model_instance, add):
        if add:
            sf = SnowflakeGenerator(1)
        else:
            return super().pre_save(model_instance, add)

I am making a base model all my other models will mixin/inherit from:


class BaseMixin(models.Model):
    """Mixin/Abstract class for all models to inherit from to add common fields (id, datelogged, lastupdated)"""
    id = SnowflakeField()
    datelogged = models.DateTimeField(auto_now_add=True)
    lastupdated = models.DateTimeField(auto_now=True)

    #system = models.BooleanField(default=False)
    # we can use is_superuser for this

    class Meta:
        abstract = True

I know that django gives your row an ID already, is it possible to overwrite it (like i did it above)?

Snowflake IDs, or snowflakes, are a form of unique identifier used in distributed computing. The format was created by Twitter and is used for the IDs of tweets. It is popularly believed that every snowflake has a unique structure, so they took the name "snowflake ID". The format has been adopted by other companies, including Discord and Instag...

native schooner
#

Making my own Snowflake field

torpid shadow
#

Have you considered if this approach is process safe? The Auto ID is generated by the DBMS, which guarantees uniqueness and is process safe.

native schooner
#

ID

torpid shadow
#

Snowflake IDs are the current time in milliseconds + a sequence number. So if you have two processes running in parallel and both producing an ID at the same time, you get the same ID. Since Django usually processes multiple requests in parallel (e.g. with gunicorn you have multiple worker processes running), you have this problem.