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.

[Support request] No primary key for a model

See original GitHub issue

Hi Charles,

I’ve got a table which logs keywords, schema is:

class Keyword(Model):
    name = CharField(null=False)
    created = DateTimeField(null=False, default=datetime.utcnow)

    class Meta:
        indexes = (
            (('created', 'name'), False)
        )

In short, I want to drop the auto-generated primary key column (id). How do I prevent it from being readded in subsequent db creations? I’ve taken a look into the manual and not found the syntax.


In long, in the database however it has added an id column and associated pkey index. This table now has 35 million rows and that pkey index taking up 10gb in ram. I’ve taken a look into the code and it looks safe to drop the column, we only perform an aggregate query on the table in any case:

Keyword.select(Keyword.created, Keyword.name).where(Keyword.created > 3_days) OR Keyword.select().where(Keyword.created > 3_days).count()

Neither of these should need a primary key as we can get exact duplicates (35mil rows are for 3 days of data).

Many thanks, Alex

PS. It would also be awesome to be able to name index constraints, getting a few problems when running schema migrations because of name mismatches between environments.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
coleifercommented, Jun 15, 2015

For models with no primary key, simply specify primary_key=False in the inner Meta class:

class Keyword(Model):
    name = CharField(null=False)
    created = DateTimeField(null=False, default=datetime.utcnow)

    class Meta:
        primary_key = False

If you want a multi-column primary key you can write:

class Keyword(Model):
    name = CharField(null=False)
    created = DateTimeField(null=False, default=datetime.utcnow)

    class Meta:
        primary_key = CompositeKey('name', 'created')
0reactions
perluncommented, Sep 25, 2018

Thanks @coleifer for this answer of yours - it helped me greatly now, three years later. 👍 (I was struggling with peewee trying to retrieve data from a non-existing sequence as below)

psycopg2.ProgrammingError: relation "issue_assignees_id_seq" does not exist
LINE 1: SELECT CURRVAL('"issue_assignees_id_seq"')

Applying the primary_key = False to the inner class fixed the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework: table without primary key - Stack Overflow
Some tables do not have primary keys defined so that when I create a new Entity Data Model, I get the following message:...
Read more >
Understand how to choose a primary key for a DynamoDB table
Partition key: This is a simple primary key. If the table has only a partition key, then no two items can have the...
Read more >
Keyless Entity Types - EF Core - Microsoft Learn
In addition to regular entity types, an EF Core model can contain keyless ... Mapping to database views that do not contain a...
Read more >
Error: Non-Unique value/primary key (or sql_distinct_key ...
Quick fix. If the counts in this query match, the primary key is unique. If the counts do not match, the primary key...
Read more >
ORM Configuration - SQLAlchemy 1.4 Documentation
Most ORMs require that objects have some kind of primary key defined because the object in memory must correspond to a uniquely identifiable...
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