Tortoise-orm: Support auto generate UUID for non pk field

Created on 30 Jan 2020  路  7Comments  路  Source: tortoise/tortoise-orm

First, Thank you for support nice software :)
I find that UUIDField only auto generate UUID4 only that field is primary key.
How about support auto generate UUID key for non pk field?

Maybe we can use like below

uid = fields.UUIDField(auto_generate=True) # default : False

Also, I'm not good at orm and database well. I want to hear about other's opinions.
I really appreciate if anyone answer to me! Thank you :+1:

documentation

Most helpful comment

Hi!

Actually, for pk uuid fields generation is done inside of Tortoise just like that:

    def __init__(self, **kwargs) -> None:
        if kwargs.get("pk", False):
            if "default" not in kwargs:
                kwargs["default"] = uuid4
        super().__init__(**kwargs)

So, if you wanna same behaviour for non-pk uuid field you just have to do it like that:

from uuid import uuid4
...
uid = fields.UUIDField(default=uuid4)

All 7 comments

Hi!

Actually, for pk uuid fields generation is done inside of Tortoise just like that:

    def __init__(self, **kwargs) -> None:
        if kwargs.get("pk", False):
            if "default" not in kwargs:
                kwargs["default"] = uuid4
        super().__init__(**kwargs)

So, if you wanna same behaviour for non-pk uuid field you just have to do it like that:

from uuid import uuid4
...
uid = fields.UUIDField(default=uuid4)

@abondar Thank you for your friendly comment :)

What do you think about adding new keywords to UUIDField for auto-generate UUID for non pk field? Adding new keyword can be useful to user and also prevent unnecessary uuid import.

If you do not agree, then how about add some example code about this issue? I think it is hard to find upper solution just looking document. We must view source code to solve upper problem.

Thank u and Have a nice day ! :smile:

Well, I think there really is issue with default parameter for fields documentation. It can accept functions as value and this function will be called if field has to be populated by default value.

It isn't documented in field's reference in docs, which, I think, happened mostly because it's behaviour emulates Django's same parameter, so it hasn't bothered us before.
I think we will have to improve docs in this terms.

Regarding auto-generate parameter I would oppose such solution, because it's seems to be too specific and would bring more confusion to users, than be really helpful, cause people will have to find out what exactly this params mean and it means we would have to document it thoroughly.

But if we gonna document something I think we should better document default param behaviour :)

Yes, I have been having to do many support queries where the docs either had something missing, or was hard to find. We need doc guidelines.

Well, I think there really is issue with default parameter for fields documentation.

I agree with above opinion. So i will close this issue after documentation updated. Thank u :)

Documented default and all standard field params better: https://tortoise-orm.readthedocs.io/en/latest/fields.html#module-tortoise.fields.base

Hi, tortoise.exceptions.ConfigurationError: Field 'agt_pk' (UUIDField) can't be DB-generated is thrown when

class Agent(Model):
    agt_pk = fields.UUIDField(pk=True, generated=True)

Successfully installed tortoise-orm-0.16.21

Documentation says that
If UUID Field is used as a primary key, it will auto-generate a UUID4 by default.

I found out that this is true only if you omit (generated=True). In that case it should be clear to the user that pk generation is done inside Tortoise NOT inside Postgres. But it is possible to have both.

CREATE EXTENSION IF NOT EXISTS "uuid-ossp"
ALTER TABLE "Agent" ALTER COLUMN agt_pk SET DEFAULT uuid_generate_v4();
Was this page helpful?
0 / 5 - 0 ratings