Sobes.tech
Middle+

// The user may own multiple electronic wallets in different currencies, but cannot own two wallets in the same currency. // Possible currencies: USD/EUR/RUB. The wallet must contain the current balance; // Currencies should have an additional name in Russian. // ADD Currency -> BTC/ETH/TON // ADD language ES/FR/EN User { id int PK string email string } Currency { code varchar(3) PK // USD, EUR, RUB name_ru string optional type enum('fiat', 'crypto') } CurrencyTranslations { } Wallet { user_id FK (User.id) currency_code FK (Currency.code) balance decimal }

sobes.tech AI

Answer from AI

To implement the data model considering the requirements:

  • A user can have multiple wallets, but no more than one in each currency.
  • Currencies have a code, type (fiat or crypto), and names in different languages.

The following structure is proposed:

User {
  id INT PRIMARY KEY,
  name VARCHAR(255),
  email VARCHAR(255)
}

Currency {
  code VARCHAR(3) PRIMARY KEY, -- USD, EUR, RUB, BTC, ETH, TON
  type ENUM('fiat', 'crypto')
}

CurrencyTranslations {
  currency_code VARCHAR(3) REFERENCES Currency(code),
  language_code VARCHAR(2), -- ru, en, es, fr, etc.
  name VARCHAR(255),
  PRIMARY KEY (currency_code, language_code)
}

Wallet {
  user_id INT REFERENCES User(id),
  currency_code VARCHAR(3) REFERENCES Currency(code),
  balance DECIMAL(18, 2),
  PRIMARY KEY (user_id, currency_code) -- ensures unique currency per user
}

Explanation:

  • The Currency table stores all currencies with their type.
  • In CurrencyTranslations, each currency and language has a name, for example, "US Dollar" in Russian for USD.
  • The Wallet table creates a many-to-many relationship between users and currencies with a uniqueness constraint on (user_id, currency_code) to prevent a user from having two wallets in the same currency.

Adding new currencies (BTC, ETH, TON) and languages (ES, FR, EN) is done by adding records to the respective Currency and CurrencyTranslations tables.

// The user may own multiple electronic wallets in… - sobes.tech