Blog ·
Modelling a multi-game card collection with stable ids
How to store cards from several card games in one schema: which id to key on, what to keep from the API, and where copies and prices belong.
A collection app built for one game usually keys its cards on something the game prints: a set code and a number, or a name. That holds until the second game arrives. This article describes a schema that keeps working when the second game is added, and the nineteenth.
Key on the id, not on what is printed
Every card the API returns has an id. It never changes and is never reused. Make it the primary key of your card table and the foreign key everywhere else.
Printed fields are good for search and bad as keys. A few cases from the catalogue:
- A Yu-Gi-Oh! card printed at three rarities is three cards with three ids. The number of the first is MAMS-EN001; the others add a letter, MAMS-EN001a and MAMS-EN001b.
- A Gundam Card Game card reprinted in a later set keeps the number it was first printed with, so the set GD06 can hold a card numbered GD02. The number does not tell you the set.
- Cyberpunk TCG numbers count within each product: 001 in a starter deck and 001 in the booster are two different cards.
- Names repeat in every game: alternate arts, parallels and reprints share the name of the base card.
The id is the only identity the API promises. Use set and number to find a card, then store what you found by its id.
Three tables, three speeds
A collection holds three kinds of fact: what exists (the catalogue), what a user owns (copies), and what a marketplace asked or sold for on a given day (quotes). They change at different speeds and come from different places, so they belong in different tables.
-- What exists. Filled from the API, never edited by users.
create table card (
id uuid primary key, -- data.id: never changes, never reused
game text not null, -- data.game: pokemon, onepiece, yugioh…
set_code text not null, -- data.set.code
number text not null, -- data.number, exactly as returned
name text not null,
rarity text,
supertype text,
attributes jsonb, -- game-specific rules data
image_url text,
cardmarket_id integer, -- data.external_ids.cardmarket, may be null
tcgplayer_id integer, -- data.external_ids.tcgplayer, may be null
version integer not null -- data.version: moves when the card changes
);
create index on card (game, set_code, number); -- for lookups, not unique
-- What a user owns.
create table copy (
id bigserial primary key,
user_id bigint not null,
card_id uuid not null references card (id),
language text not null, -- the language this copy is printed in
printing text not null default '', -- a finish, where the game prices it apart
condition text,
quantity integer not null default 1
);
-- What a source quoted, kept with its date. The API serves the latest only.
create table quote (
card_id uuid not null references card (id),
source text not null, -- CARDMARKET, TCGPLAYER
variant text not null, -- LOWEST_NEAR_MINT, MARKET, TREND…
language text not null,
condition text not null default '',
printing text not null default '',
region text not null default '',
amount numeric not null,
currency text not null, -- EUR or USD: never summed together
as_of timestamptz not null,
primary key (card_id, source, variant, language, condition, printing, region, as_of)
);One card table holds every game. The objects have the same fields whatever the game, and the rules data that differs sits in attributes, so a new game is new rows, not a new table and a migration.
The index on game, set and number is a plain index, not a unique constraint. The API does not promise that pair is unique, and a constraint that holds for most games but not for one becomes a failed import.
A copy is not a card
A copy has properties the catalogue does not: the language it was printed in, its condition, how many the user has. Two users who own the same card own two copies of one card row.
Two cases need care, and both are decided by the catalogue, not by your schema:
- Asian Pokémon print lines are sets of their own, with region EAST, their own codes, numbers and images. A Japanese printing is a different card with a different id, not a copy of the English card with language set to ja.
- Finishes are a separate card in some games and a property of the copy in others. In Yu-Gi-Oh! each rarity has its own id. In Riftbound foil and non-foil are one card, and their prices are told apart by the printing field of each quote. Where a finish has its own id, it is a card; where it does not, store it on the copy and match it to printing.
Keeping the catalogue current
- For each set your users hold, read /v1/sets/{code}/cards page by page and compare version with the value you stored. Update only the rows whose version moved.
- To find new sets, list /v1/sets?game=… newest first and add the codes you do not have. Read the whole list again from time to time: a set can be added with a release date older than your newest one.
- A list costs 1 credit for every 50 rows requested, so re-reading a set with limit=250 costs 5 credits a page. A detail request costs 1.
- Load the list of games from /v1/games at start-up instead of hard-coding it. A game the API adds later then appears without a release of your app.
Joining with marketplace data
external_ids carries the Cardmarket and TCGplayer product ids. Treat both as optional. In some games TCGplayer lists each finish as a product of its own while Cardmarket groups them under one card, so the Cardmarket id is missing wherever the two catalogues do not agree on the printing.
Quotes come from /v1/cards/{id}/prices at 2 credits a card. For many cards at once, /v1/prices/current takes up to 25 ids for 4 credits. You do not have to build a history yourself: /v1/cards/{id}/prices/history returns each series day by day. If you keep your own copy of the quotes, store each one with its as_of and insert a row only when as_of is newer than the last one you hold.
curl "https://api.alltcgapi.com/v1/cards/{id}/prices?source=CARDMARKET" \
-H "X-Api-Key: $ALLTCG_KEY"Checklist
- Primary key: the API id. Never a name, never set plus number.
- One card table for every game, with game as a column and attributes as JSON.
- Copies in their own table, with language, condition and, where needed, printing.
- Quotes in their own table, with source, currency and as_of on every row.
- Sync by version; discover games and sets from the API, not from a list in your code.
Published · Atom feed