# ledger_account

## Description

<details>
<summary><strong>Table Definition</strong></summary>

```sql
CREATE TABLE `ledger_account` (
  `ledger_account_id` mediumint unsigned NOT NULL AUTO_INCREMENT,
  `abacus_event_id` mediumint unsigned NOT NULL,
  `account_id` mediumint unsigned NOT NULL,
  `contract_id` mediumint unsigned DEFAULT NULL,
  `currency_code` char(3) COLLATE utf8mb4_general_ci NOT NULL,
  `currency_amount` decimal(20,2) NOT NULL,
  `previous_balance` decimal(20,2) NOT NULL,
  `current_balance` decimal(20,2) NOT NULL,
  `note` varchar(255) COLLATE utf8mb4_general_ci DEFAULT NULL,
  `created_by` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
  `created_at` datetime NOT NULL,
  `last_modified_by` varchar(255) COLLATE utf8mb4_general_ci NOT NULL,
  `last_modified` datetime NOT NULL,
  PRIMARY KEY (`ledger_account_id`),
  UNIQUE KEY `uidx_account_contract_event` (`abacus_event_id`,`account_id`,`contract_id`),
  KEY `fk_ledger_account_account` (`account_id`),
  KEY `fk_ledger_account_contract` (`contract_id`),
  KEY `fk_ledger_account_abacus_event` (`abacus_event_id`),
  CONSTRAINT `fk_ledger_account_abacus_event` FOREIGN KEY (`abacus_event_id`) REFERENCES `abacus_event` (`abacus_event_id`) ON DELETE RESTRICT ON UPDATE RESTRICT,
  CONSTRAINT `fk_ledger_account_account` FOREIGN KEY (`account_id`) REFERENCES `account` (`account_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_ledger_account_contract` FOREIGN KEY (`contract_id`) REFERENCES `contract` (`contract_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=[Redacted by tbls] DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci
```

</details>

## Columns

| Name              | Type               | Default | Nullable | Extra Definition | Children                                                            | Parents                         | Comment |
| ----------------- | ------------------ | ------- | -------- | ---------------- | ------------------------------------------------------------------- | ------------------------------- | ------- |
| abacus_event_id   | mediumint unsigned |         | false    |                  |                                                                     | [abacus_event](abacus_event.md) |         |
| account_id        | mediumint unsigned |         | false    |                  |                                                                     | [account](account.md)           |         |
| contract_id       | mediumint unsigned |         | true     |                  |                                                                     | [contract](contract.md)         |         |
| created_at        | datetime           |         | false    |                  |                                                                     |                                 |         |
| created_by        | varchar(255)       |         | false    |                  |                                                                     |                                 |         |
| currency_amount   | decimal(20,2)      |         | false    |                  |                                                                     |                                 |         |
| currency_code     | char(3)            |         | false    |                  |                                                                     |                                 |         |
| current_balance   | decimal(20,2)      |         | false    |                  |                                                                     |                                 |         |
| last_modified     | datetime           |         | false    |                  |                                                                     |                                 |         |
| last_modified_by  | varchar(255)       |         | false    |                  |                                                                     |                                 |         |
| ledger_account_id | mediumint unsigned |         | false    | auto_increment   | [ledger_account_current_balance](ledger_account_current_balance.md) |                                 |         |
| note              | varchar(255)       |         | true     |                  |                                                                     |                                 |         |
| previous_balance  | decimal(20,2)      |         | false    |                  |                                                                     |                                 |         |

## Constraints

| Name                           | Type        | Definition                                                                        |
| ------------------------------ | ----------- | --------------------------------------------------------------------------------- |
| PRIMARY                        | PRIMARY KEY | PRIMARY KEY (ledger_account_id)                                                   |
| fk_ledger_account_abacus_event | FOREIGN KEY | FOREIGN KEY (abacus_event_id) REFERENCES abacus_event (abacus_event_id)           |
| fk_ledger_account_account      | FOREIGN KEY | FOREIGN KEY (account_id) REFERENCES account (account_id)                          |
| fk_ledger_account_contract     | FOREIGN KEY | FOREIGN KEY (contract_id) REFERENCES contract (contract_id)                       |
| uidx_account_contract_event    | UNIQUE      | UNIQUE KEY uidx_account_contract_event (abacus_event_id, account_id, contract_id) |

## Indexes

| Name                           | Definition                                                                                    |
| ------------------------------ | --------------------------------------------------------------------------------------------- |
| PRIMARY                        | PRIMARY KEY (ledger_account_id) USING BTREE                                                   |
| fk_ledger_account_abacus_event | KEY fk_ledger_account_abacus_event (abacus_event_id) USING BTREE                              |
| fk_ledger_account_account      | KEY fk_ledger_account_account (account_id) USING BTREE                                        |
| fk_ledger_account_contract     | KEY fk_ledger_account_contract (contract_id) USING BTREE                                      |
| uidx_account_contract_event    | UNIQUE KEY uidx_account_contract_event (abacus_event_id, account_id, contract_id) USING BTREE |

## Triggers

| Name                        | Definition                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| --------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| after_ledger_account_insert | CREATE TRIGGER after_ledger_account_insert AFTER INSERT ON ledger_account<br />FOR EACH ROW<br />BEGIN<br />    INSERT INTO `ledger_account_current_balance`<br />        (ledger_account_id, account_id, created_by, created_at, last_modified_by, last_modified)<br />    VALUES<br />        (NEW.ledger_account_id, NEW.account_id, NEW.created_by, NOW(), NEW.last_modified_by, NOW())<br />    ON DUPLICATE KEY UPDATE<br />        ledger_account_id = NEW.ledger_account_id,<br />        last_modified_by = NEW.last_modified_by,<br />        last_modified = NOW();<br />END |

## Relations

![er](ledger_account.svg)

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
