piccolo
piccolo copied to clipboard
Support for other primary key names than `id`?
So I'm creating a new table with varchar primary key which has another name than id
.
import piccolo.columns as cl
from piccolo.table import Table
class Currency(Table):
code = cl.Varchar(primary_key=True)
The migration file created successfully, but executing it results in the following error:
File "asyncpg/protocol/protocol.pyx", line 201, in bind_execute
asyncpg.exceptions.InvalidTableDefinitionError: multiple primary keys for table "currency" are not allowed
Apparently, piccolo adds the default id
serial field along with the expressly stated one.
So I removed it from the migration file and the migration executes fine. But after that trying to select this table leads to the following error:
File "asyncpg/protocol/protocol.pyx", line 168, in prepare
asyncpg.exceptions.UndefinedColumnError: column currency.id does not exist
Piccolo still tries to select the id
field even if it isn't defined in the model.
So I tried to trick it with db_column_name='id'
param. But this doesn't work either:
asyncpg.exceptions.DuplicateColumnError: column "id" specified more than once
Hmm, interesting.
It must be something with migrations. I think there may be a problem if you add a table with the default primary key, and then change it to a custom one.
As for selects, if I do something simple like this in the playground it works:
class MyTable(Table):
pk = Varchar(primary_key=True)
>>> await MyTable.create_table()
>>> await MyTable.insert(MyTable(pk='abc'))
>>> await MyTable.select()
[{'pk': 'abc'}]
So the bug probably isn't with select, but with the creation of the tables.
No, I wasn't changing the primary key, I wanted to create new table with primary key named differently than id
. Via migration. And it didn't work.
OK, thanks for the clarification. I'll have to setup a test project and try it.