Replies: 4 comments
-
|
I think we should probably add an The downside is migrations would have to be aware of this, and if the user changes |
Beta Was this translation helpful? Give feedback.
-
|
Any news on this issue? |
Beta Was this translation helpful? Give feedback.
-
I made a small workaround: import random
from piccolo.table import Table
@classmethod
def _get_index_name(cls, column_names: list[str]) -> str:
"""
Generates a semi-unique index name from the table name and column names.
"""
hex_range = range(16)
random_symbols = ''.join([f'{random.choice(hex_range):01x}' for i in range(4)])
return '_'.join([cls._meta.tablename] + column_names + ['idx', random_symbols])
Table._get_index_name = _get_index_nameHowever, in this case you will not be able to correctly undo the migration and will have to delete the index manually if necessary. |
Beta Was this translation helpful? Give feedback.
-
|
Thx @metakot i also use sth similar now: from pathlib import Path
from piccolo.conf.apps import AppConfig, table_finder
from piccolo.table import Table
# HACK See https://github.com/piccolo-orm/piccolo/issues/990
@classmethod
def _get_index_name(cls: type[Table], column_names: list[str]) -> str:
name_parts: list[str] = [cls._meta.tablename, *column_names, "idx"] # pyright: ignore[reportPrivateUsage]
index_name = "_".join(name_parts)
return index_name
Table._get_index_name = _get_index_name # pyright: ignore[reportAttributeAccessIssue, reportPrivateUsage] # noqa: SLF001
CURRENT_FOLDER_PATH = Path(__file__).parent.absolute()
APP_CONFIG = AppConfig(
app_name="xxx",
migrations_folder_path=CURRENT_FOLDER_PATH / "piccolo_migrations",
table_classes=table_finder(modules=["xxx.entities"]),
migration_dependencies=[],
commands=[],
) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
So I have this two tables in postgres:
The migration file is created successfully, but the migration itself ends with the error:
asyncpg.exceptions.DuplicateTableError: relation "employee_role" already existsIt turns out that piccolo generates the following SQL:
CREATE INDEX employee_role ON "employee" USING btree ("role");And this clashes with the name of the table.
The code for index name lives here:
To prevent that kind of name conflicts, can we add the suffix to the index name, like
_idx_XXXXthere X is the random hex symbol?UPD: however, in order to be able to gracefully undo the migration backwards the migration file must store the index name somethere or look it up from the database.
Beta Was this translation helpful? Give feedback.
All reactions