a8m / documentdb

Go driver for Microsoft Azure DocumentDB
MIT License
33 stars 25 forks source link

PartitionKey extracted from document doesn't match the one specified in the header #32

Closed ben790924 closed 2 years ago

ben790924 commented 3 years ago

I use documentdb.CreateDocument method and It logged this message below:

BadRequest, Message: {"Errors":["PartitionKey extracted from document doesn't match the one specified in the header"]}

And this is my code:

     `info := infoDoc{}
if err := ctx.BodyParser(&info); err != nil {
    fmt.Println(err)
}

resp, err := database.Client.CreateDocument(
    "dbs/xxxx/colls/xxxxxxx/",
    &info,
    documentdb.Upsert(),
    documentdb.PartitionKey("pk"),
)
if err != nil {
    log.Fatal("createDocument Error: \t", err)
}

fmt.Println(resp)

return ctx.JSON(info)`

The same way using type documentdb methods like QueryDocument、QueryCollections works fine.

Also, I'm new to GO and I've searched all over the internet but nothing helps.

Any help will be appreciated

berndverst commented 2 years ago

This is a pretty straightforward issue -- when you created your Cosmos DB table you had to specify a partitionKey path, let's say /myPartitionKey

When you insert documents into Cosmos DB you need to ensure your document contains this key at the right level, so in this example there has to be a top level attribute myPartitionKey. Additionally, you need to provide the partition key via the documentdb.PartitionKey param.

ben790924 commented 2 years ago

It's been a long time since then. I've solved this problem. Still, Thanks for your explanations =)