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.

Primary Key missing in auto-generated migration

See original GitHub issue

Using Postgres & Piccolo 0.32.0, the auto-generated migrations fail to include the primary key.

Schema:

from piccolo.table import Table
from piccolo.columns import ForeignKey, UUID

class Types(Table):
    id = UUID(primary_key=True)

class Things(Table):
    type = ForeignKey(references=Types)

When I try to run migrations via:

piccolo migrations new my_app --auto
piccolo migrations forwards all

I get:

Running MigrationManager ...
The command failed.
foreign key constraint "things_type_fkey" cannot be implemented
DETAIL:  Key columns "type" and "id" are of incompatible types: integer and uuid.

Looking at the generated migration, I see:

class Types(Table, tablename="types"):
    pass

Adding the PK manually to the migration fixes the issue:

class Types(Table, tablename="types"):
    id = UUID(primary_key=True)

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
dantownsendcommented, Aug 20, 2021

I think the issue here is the ForeignKey should be created with type UUID, otherwise it can’t store the reference to a UUID primary key.

This should work - I’ll investigate what went wrong.

1reaction
aminalaeecommented, Aug 20, 2021

@adriangb That’s fine I think. We didn’t really have an issue for that PR. Thanks for reporting it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use Table in Entity with no Primary Key (Using Entity Code ...
1 Answer 1 ... If the field contains unique data you can mark it as Key in EF and disable all automatic migrations....
Read more >
Auto Generating Migrations — Alembic 1.9.0 documentation
Autogenerate can't currently, but will eventually detect: Some free-standing constraint additions and removals may not be supported, including PRIMARY KEY, ...
Read more >
Auto-generated primary keys: UUID, serial or identity column?
This article explores the old question what to use for autogenerated primary keys: UUID, serial or identity column?
Read more >
UUID primary key leads to delayed anonymous unique ...
Describe the bug Summary: When using UUID as primary key in SQL Alchemy, ... autogenerates again, the constraint continues to be "missing".
Read more >
Troubleshooting migration tasks in AWS Database Migration ...
To solve this issue, the current approach is to first precreate the tables and primary keys in the target database. Then use a...
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