question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PrimaryKeyCreator, no parameter are passed to the create_pk method

See original GitHub issue

I want to make an MurmurHash class to create a primary key for a new model instance. I did this class that adheres to the PrimaryKeyCreator protocol:

# https://github.com/hajimes/mmh3
import mmh3

class Mmh3PrimaryKey:
    """
    A client-side generated primary key that follows the MurmurHash (MurmurHash3) spec.
    https://en.wikipedia.org/wiki/MurmurHash
    """

    @staticmethod
    def create_pk(self, *args, **kwargs) -> str:
        return str('some argument in args / kwargs')

and set Meta primary_key_creator_cls like this:

class ErrorServer(HashModel):
    local_hostname: str
    class Meta:
        primary_key_creator_cls = Mmh3PrimaryKey

But when I instantiate ErrorServerclass, no parameter (len of *args, **kwargs == 0) are passed to create_pk

es = ErrorServer(local_hostname='my_hostname', param2='Test')

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
DasLukascommented, Sep 11, 2022

I also need to change the default pk. I use this workaround. Works fine for me.

from pydantic import root_validator

class Foo(JsonModel):
    name: str

    @root_validator(pre=True)
    def overwrite_pk(cls, values: dict[str, Any]):
        def _overwrite_pk(*args, **kwargs):
            return f"{args[0]}.{args[1]}"

        values["pk"] = _overwrite_pk(values["name"])

        return values
foo = Foo(name="test")

print(foo.pk) # -> "test"
1reaction
nicolas-rdgscommented, May 29, 2022

I am also interested in changing the default primary key and noticed the same behavior.

I also noticed that the custom class FieldInfo (inherited from Pydantic FieldInfo) has the attribute “primary_key” which is used in the RedisModel class to initiate the default field pk as primary key.

But apparently this attribute cannot be used elsewhere:

Class Customer(HashModel):
    email: EmailStr = Field(index=True, primary_key=True)

--
File ~/.local/lib/python3.9/site-packages/redis_om/model/model.py:1158, in RedisModel.validate_primary_key(cls)
   1156     raise RedisModelError("You must define a primary key for the model")
   1157 elif primary_keys > 1:
-> 1158     raise RedisModelError("You must define only one primary key for a model")

RedisModelError: You must define only one primary key for a model

It would be great to use this parameter like SQL Alchemy ORM to change the default behavior. If we use this parameter manually in another field (declared one time in the model), the latter must have precedence over the pk field. What do you think?

Read more comments on GitHub >

github_iconTop Results From Across the Web

SerialSerialKeyCreator (Oracle - Berkeley DB Java Edition API)
A abstract key creator that uses a serial key and a serial data entry. ... Creates the index key object from primary key...
Read more >
PUT parameter into IDENTITY(1,1) property in SQL Table ...
CREATE TABLE Pets ( PetId int IDENTITY(@Parameter,1) PRIMARY KEY, PetName varchar(255) );. My SQL parser does not accept such syntax. sql-server ...
Read more >
CREATE FUNCTION (Transact-SQL) - Microsoft Learn
Specify a parameter name by using an at sign (@) as the first character. The parameter name must comply with the rules for...
Read more >
Example: CREATE Request with Presence or Absence of PRIMARY ...
This example specifies neither an explicit PRIMARY INDEX clause nor an explicit NO PRIMARY INDEX clause, but does specify a UNIQUE constraint on...
Read more >
Db2 12 - Db2 SQL - CREATE TABLE - IBM
Provides a shorthand method of defining a primary key composed of a single column. Thus, if PRIMARY KEY is specified in the definition...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found