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

[Usage] how to create orm class without default id column #88

Open
flyly0755 opened this issue Jun 14, 2024 · 3 comments
Open

[Usage] how to create orm class without default id column #88

flyly0755 opened this issue Jun 14, 2024 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@flyly0755
Copy link

class Event(models.ClickhouseModel):
    class Action(IntegerChoices):
        PASS = 1
        DROP = 2
        ALERT = 3
    ip = models.GenericIPAddressField(default="::")
    ipv4 = models.IPv4Field(default="127.0.0.1")
    ip_nullable = models.GenericIPAddressField(null=True)
    port = models.UInt16Field(default=0)
    protocol = models.StringField(default="", low_cardinality=True)
    content = models.JSONField(default=dict)
    timestamp = models.DateTime64Field(default=timezone.now)
    created_at = models.DateTime64Field(auto_now_add=True)
    action = models.EnumField(choices=Action.choices, default=Action.PASS)

    class Meta:
        ordering = ["-timestamp"]
        engine = models.MergeTree(
            primary_key="timestamp",
            order_by=("timestamp", "id"),
            partition_by=models.toYYYYMMDD("timestamp"),
            index_granularity=1024,
            index_granularity_bytes=1 << 20,
            enable_mixed_granularity_parts=1,
        )
        indexes = [
            models.Index(
                fields=["ip"],
                name="ip_set_idx",
                type=models.Set(1000),
                granularity=4
            ),
            models.Index(
                fields=["ipv4"],
                name="ipv4_bloom_idx",
                type=models.BloomFilter(0.001),
                granularity=1
            )
        ]
        constraints = (
            CheckConstraint(
                name="port_range",
                check=Q(port__gte=0, port__lte=65535),
            ),
        )

Use code as above will create table event with a default id column, how to create without this id column?

@flyly0755 flyly0755 added the bug Something isn't working label Jun 14, 2024
@jayvynl
Copy link
Owner

jayvynl commented Jun 15, 2024

specify a primary key field explicitly

@flyly0755
Copy link
Author

flyly0755 commented Jun 17, 2024

specify a primary key field explicitly

yes, specify primary key filed explicitly like FILE_ID = models.UInt32Field(primary_key=True), and set in class Meta as

    class Meta:
        engine = models.MergeTree(
            primary_key="FILE_ID",
            order_by=("FILE_ID",),
            index_granularity=8192,
        )

But still with default column id in the table.

@jayvynl
Copy link
Owner

jayvynl commented Jun 17, 2024

I mean choose a field and set primary_key=True like:

class Event(models.ClickhouseModel):
    ip = models.GenericIPAddressField(default="::", primary_key=True)

If the so called primary key field is not unique, then query method get or save or other methods depend on unique primary key is not expected to be called.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants