Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use UUID as _id #374

Open
GoriilaGrodd opened this issue Apr 18, 2022 · 3 comments
Open

How to use UUID as _id #374

GoriilaGrodd opened this issue Apr 18, 2022 · 3 comments
Labels

Comments

@GoriilaGrodd
Copy link

I want to use UUID for the _id value, but I can't get Mongo to recognise the value.

I start by setting uuidRepresentation='standard' parameter when initiating the MongoClient connection.

In my schema I declare a UUID attribute; this seemed to have no effect:

uuid = fields.UUIDField(
	attribute='_id',
	default=uuid.uuid4,
	unique=True,
	)

Result: 
{
  "_id": {
    "$oid": "625aadba5d4a05bfa5c6b26b"
  },
}

Declaring the attribute as a string, the result is the same:

uuid = fields.StringField(
	attribute='_id',
	default=str(uuid.uuid4()),
	unique=True,
	)

Result: 
{
  "_id": {
    "$oid": "625aaad798ceb63062a2c15c"
  },
}

What am I doing wrong?

@vlad-watcher
Copy link

vlad-watcher commented Apr 20, 2022

If you want your UUID to be as _id, you need :

  1. create some field in document
  2. specify that this field is primary_key
`uuid = fields.StringField(
	attribute='_id',
	default=str(uuid.uuid4()),
	unique=True,
        primary_key=True
	)

Result: 
{
  "_id": {
    "$oid": "625aaad798ceb63062a2c15c"
  },
}`

Then this field value will be stored as _id of your object
I hope this will work for you

@GoriilaGrodd
Copy link
Author

Thanks for the assistance. I've now worked out the problem - when I use inheritance umongo/Mongo ignores my attribute:

@instance.register
class SeedModel(Document):
    glossary_uuid = fields.UUIDField(
        required=True,
        )

    class Meta:
        abstract = True

class SeriesModel(SeedModel):
    uuid = fields.UUIDField(
        attribute='_id',
        default=uuid.uuid4(),
        unique=True,
        primary_key=True,
        )
    class Meta:
        collection_name = "things"

_new_series = SeriesModel()
Result:
{
	"_id": {
		"$oid": "62603f8ed4f3d3fedd273e4b"
	},
}

If SeriesModel inherits directly from Document then the result is as I originally desired - an UUID value for the _id :

class SeriesModel(Document):
    uuid = fields.UUIDField(
        attribute='_id',
        default=uuid.uuid4(),
        unique=True,
        primary_key=True,
        )
    class Meta:
        collection_name = "things"

_new_series = SeriesModel()
Result:
{
	"_id": {
		"$binary": "R/2Me/GtQBCmV/eR/oBw5Q==",
		"$type": "4"
	},
}

Is this the correct behaviour for inheritance, or is there another parameter I need to set in order to use inheritance?

Cheers

@whophil
Copy link
Collaborator

whophil commented Apr 20, 2022

I don't know if it is related to this issue, but

default=uuid.uuid4()

should probably be

default=uuid.uuid4

See #373 #374

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants