Skip to content

Commit

Permalink
remove field.position unique constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
yedpodtrzitko committed Sep 9, 2024
1 parent 60bf0f2 commit 3e4153c
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 14 deletions.
11 changes: 2 additions & 9 deletions tagstudio/src/core/library/alchemy/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from enum import Enum
from typing import Any, TYPE_CHECKING

from sqlalchemy import ForeignKey, ForeignKeyConstraint
from sqlalchemy import ForeignKey
from sqlalchemy.orm import Mapped, mapped_column, relationship, declared_attr

from .db import Base
Expand Down Expand Up @@ -39,7 +39,7 @@ def entry(cls) -> Mapped[Entry]:

@declared_attr
def position(cls) -> Mapped[int]:
return mapped_column()
return mapped_column(default=0)

def __hash__(self):
return hash(self.__key())
Expand All @@ -66,13 +66,6 @@ def __eq__(self, value) -> bool:

class TextField(BaseField):
__tablename__ = "text_fields"
# constrain for combination of: entry_id, type_key and position
__table_args__ = (
ForeignKeyConstraint(
["entry_id", "type_key", "position"],
["text_fields.entry_id", "text_fields.type_key", "text_fields.position"],
),
)

value: Mapped[str | None]

Expand Down
5 changes: 0 additions & 5 deletions tagstudio/src/core/library/alchemy/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import shutil
from os import makedirs
from pathlib import Path
from random import randint
from typing import Iterator, Any, Type
from uuid import uuid4

Expand Down Expand Up @@ -632,7 +631,6 @@ def add_entry_field_type(
field_model = TextField(
type_key=field.key,
value=value or "",
position=randint(100, 100_000),
)
elif field.type == FieldTypeEnum.TAGS:
field_model = TagBoxField(
Expand All @@ -656,9 +654,6 @@ def add_entry_field_type(
try:
for entry_id in entry_ids:
field_model.entry_id = entry_id
# create random value position to avoid IntegrityError, reordering is below
field_model.position = randint(100, 100_000)

session.add(field_model)
session.flush()

Expand Down

0 comments on commit 3e4153c

Please sign in to comment.