Separate PokeAPI ID from national dex for correct form identification

Pokemon forms (e.g., Alolan Rattata) had their PokeAPI ID (10091) stored as
national_dex, causing them to display incorrectly. This renames the unique
identifier to pokeapi_id and adds a real national_dex field shared between
forms and their base species, so Alolan Rattata correctly shows as #19.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-07 14:55:06 +01:00
parent cb027e5215
commit d168d99bba
46 changed files with 23459 additions and 22289 deletions

View File

@@ -43,7 +43,7 @@ async def list_pokemon(
total = (await session.execute(count_query)).scalar() or 0
# Get paginated items
items_query = base_query.order_by(Pokemon.national_dex).offset(offset).limit(limit)
items_query = base_query.order_by(Pokemon.national_dex, Pokemon.name).offset(offset).limit(limit)
result = await session.execute(items_query)
items = result.scalars().all()
@@ -67,11 +67,12 @@ async def bulk_import_pokemon(
for item in items:
try:
existing = await session.execute(
select(Pokemon).where(Pokemon.national_dex == item.national_dex)
select(Pokemon).where(Pokemon.pokeapi_id == item.pokeapi_id)
)
pokemon = existing.scalar_one_or_none()
if pokemon is not None:
pokemon.national_dex = item.national_dex
pokemon.name = item.name
pokemon.types = item.types
if item.sprite_url is not None:
@@ -82,7 +83,7 @@ async def bulk_import_pokemon(
session.add(pokemon)
created += 1
except Exception as e:
errors.append(f"Dex #{item.national_dex} ({item.name}): {e}")
errors.append(f"PokeAPI #{item.pokeapi_id} ({item.name}): {e}")
await session.commit()
return BulkImportResult(created=created, updated=updated, errors=errors)
@@ -93,12 +94,12 @@ async def create_pokemon(
data: PokemonCreate, session: AsyncSession = Depends(get_session)
):
existing = await session.execute(
select(Pokemon).where(Pokemon.national_dex == data.national_dex)
select(Pokemon).where(Pokemon.pokeapi_id == data.pokeapi_id)
)
if existing.scalar_one_or_none() is not None:
raise HTTPException(
status_code=409,
detail=f"Pokemon with national dex #{data.national_dex} already exists",
detail=f"Pokemon with PokeAPI ID #{data.pokeapi_id} already exists",
)
pokemon = Pokemon(**data.model_dump())
@@ -145,17 +146,17 @@ async def update_pokemon(
raise HTTPException(status_code=404, detail="Pokemon not found")
update_data = data.model_dump(exclude_unset=True)
if "national_dex" in update_data:
if "pokeapi_id" in update_data:
existing = await session.execute(
select(Pokemon).where(
Pokemon.national_dex == update_data["national_dex"],
Pokemon.pokeapi_id == update_data["pokeapi_id"],
Pokemon.id != pokemon_id,
)
)
if existing.scalar_one_or_none() is not None:
raise HTTPException(
status_code=409,
detail=f"Pokemon with national dex #{update_data['national_dex']} already exists",
detail=f"Pokemon with PokeAPI ID #{update_data['pokeapi_id']} already exists",
)
for field, value in update_data.items():