Skip to main content

Database Schema Reference

Database Overview

PropertyValue
DBMSMySQL 8.0.46
EngineInnoDB (default)
Character SetUTF-8 (utf8mb4)
Collationutf8mb4_unicode_ci
Production DBlgo_erp_prod
Staging DBlgo_erp_staging
Total Tables14

Connection Configuration

ParameterValue
Hostlocalhost
Userlgo
Password(stored in environment variable — never in code)
Port3306 (default)
Connection Pool Size5
Connection Timeout10,000 ms
Acquire Timeout10,000 ms
Pool Configuration:
min: 0
max: 5
idleTimeout: 60,000 ms
enableKeepAlive: true
keepAliveInitialDelay: 0

Entity Relationship Diagram

┌──────────────┐ ┌──────────────────┐ ┌──────────────┐
│ users │ │ requisitions │ │ customers │
│──────────────│ │──────────────────│ │──────────────│
│ id (PK) │◄──┐ │ id (PK) │ │ id (PK) │
│ name │ ├───│ requested_by(FK) │ │ company_name │
│ email (UQ) │ ├───│ supervisor_id(FK)│ │ contact_person│
│ password_hash│ ├───│ manager_id (FK) │ │ phone │
│ role │ ├───│ accounts_id (FK) │ │ email │
│ status │ └───│ md_id (FK) │ │ address │
│ created_at │ │ status │ │ city │
│ updated_at │ │ amount │ │ state │
└──────┬───────┘ │ category │ │ credit_limit │
│ │ priority │ │outstanding_bal│
│ │ title │ │ status │
│ │ description │ │ notes │
│ └──────────────────┘ │ created_at │
│ │ updated_at │
│ ┌──────────────────┐ └──────┬───────┘
│ │ inventory │ │
│ │──────────────────│ │
│ │ id (PK) │ │
│ │ name │ │
│ │ sku (UQ) │ │
│ │ category │ │
│ │ unit_of_measure │ │
│ │ current_stock │ │
│ │ min_stock_level │ │
│ │ unit_cost │ │
│ │ selling_price │ │
│ │ status │ │
│ │ description │ │
│ │ created_at │ │
│ │ updated_at │ │
│ └───┬───┬───┬──────┘ │
│ │ │ │ │
│ │ │ └────────────────────┐│
│ │ └──────────────┐ ││
│ │ │ ││
│ ┌───▼──────────┐ ┌────▼─────┐ ┌▼▼────────────┐
│ │ purchases │ │ sales │ │inventory_adj │
│ │──────────────│ │──────────│ │──────────────│
│ │ id (PK) │ │ id (PK) │ │ id (PK) │
│ │inventory_id │ │customer_ │ │inventory_id │
│ │supplier_name │ │ id (FK) │ │adj_quantity │
│ │ quantity │ │inventory_│ │ reason │
│ │ unit_cost │ │ id (FK) │ │ notes │
│ │ total_cost │ │ quantity │ │ adj_date │
│ │ purchase_date│ │unit_price│ │created_by(FK)│──►users
│ │ notes │ │total_amt │ │ created_at │
│ │ created_at │ │sale_date │ │ updated_at │
│ │ updated_at │ │ status │ └──────────────┘
│ └──────────────┘ │ notes │
│ │created_at│
│ ┌──────────────┐ │updated_at│
│ │production_run│ └────┬─────┘
│ │──────────────│ │
│ │ id (PK) │ ┌────▼──────────┐
│ │product_name │ │ ar_invoices │
│ │ start_date │ │────────────────│
│ │ end_date │ │ id (PK) │
│ │ batches │ │ sale_id (FK) │──►sales
│ │ input_volume │ │ invoice_number │
│ │output_volume │ │ amount │
│ │ input_cost │ │ status │
│ │ output_cost │ │ due_date │
│ │ yield_percent│ │ notes │
│ │cost_per_litre│ │ created_at │
│ │ status │ │ updated_at │
│ │ notes │ └────┬──────────┘
│ │ created_at │ │
│ │ updated_at │ ┌────▼──────────┐
│ └──────────────┘ │ ar_payments │
│ │────────────────│
│ ┌──────────────┐ │ id (PK) │
│ │ expenses │ │ invoice_id(FK) │
│ │──────────────│ │ amount │
│ │ id (PK) │ │ payment_method │
│ │ category │ │ reference_num │
│ │ description │ │ payment_date │
│ │ amount │ │ notes │
│ │payment_method│ │ created_at │
│ │ status │ │ updated_at │
│ │ vendor │ └────────────────┘
│ │ reference_num│
│ │ notes │ ┌────────────────┐
│ │ expense_date │ │ suppliers │
│ │ created_at │ │────────────────│
│ │ updated_at │ │ id (PK) │
│ └──────────────┘ │ company_name │
│ │ contact_person │
│ ┌──────────────┐ │ phone │
│ │ settings │ │ email │
│ │──────────────│ │ address │
│ │ id (PK) │ │ city │
│ │setting_key │ │ state │
│ │setting_value │ │ status │
│ │ description │ │ notes │
│ │ created_at │ │ created_at │
│ │ updated_at │ │ updated_at │
│ └──────────────┘ └────────────────┘

│ ┌────────────────┐
└──────────────►│ audit_log │
│────────────────│
│ id (PK) │
│ user_id (FK) │──►users
│ action │
│ entity_type │
│ entity_id │
│ details │
│ ip_address │
│ created_at │
└────────────────┘

Table Definitions

1. users

Purpose: Stores user accounts for system authentication and role-based access control.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique user identifier
nameVARCHAR(255)NOT NULLFull name of the user
emailVARCHAR(255)NOT NULL, UNIQUELogin email address
password_hashVARCHAR(255)NOT NULLbcrypt-hashed password
roleENUM('admin','production','sales','accountant')NOT NULLSystem role for RBAC
statusENUM('active','inactive')NOT NULL'active'Account status
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPAccount creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • UNIQUE KEY idx_users_email (email)

Foreign Keys: None

Relationships:

  • Referenced by requisitions.requested_by, requisitions.supervisor_id, requisitions.manager_id, requisitions.accounts_id, requisitions.md_id
  • Referenced by inventory_adjustments.created_by
  • Referenced by audit_log.user_id

2. settings

Purpose: Key-value store for application configuration and system settings.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique setting identifier
setting_keyVARCHAR(255)NOT NULL, UNIQUEConfiguration key name
setting_valueTEXTNOT NULLConfiguration value (may be JSON)
descriptionVARCHAR(500)NULLABLENULLHuman-readable description of the setting
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPRecord creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • UNIQUE KEY idx_settings_key (setting_key)

Foreign Keys: None

Relationships: Standalone configuration table.


3. inventory

Purpose: Master inventory of all products, raw materials, packaging, and supplies.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique inventory item identifier
nameVARCHAR(255)NOT NULLItem name
skuVARCHAR(100)NOT NULL, UNIQUEStock Keeping Unit code
categoryENUM('Edible Oil','Packaging','Raw Material','Equipment','Supplies')NOT NULLItem classification
unit_of_measureENUM('Litres','Kg','Cartons','Pieces','Rolls','Sachets')NOT NULLUnit for stock tracking
current_stockDECIMAL(12,2)NOT NULL0.00Current quantity on hand
minimum_stock_levelDECIMAL(12,2)NOT NULL0.00Reorder threshold
unit_costDECIMAL(12,2)NOT NULL0.00Average cost per unit
selling_priceDECIMAL(12,2)NOT NULL0.00Price per unit for sales
statusENUM('Active','Inactive')NOT NULL'Active'Whether item is currently tracked
descriptionTEXTNULLABLENULLDetailed item description
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPRecord creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • UNIQUE KEY idx_inventory_sku (sku)
  • KEY idx_inventory_category (category)
  • KEY idx_inventory_status (status)

Foreign Keys: None (parent table)

Relationships:

  • Referenced by purchases.inventory_id
  • Referenced by sales.inventory_id
  • Referenced by inventory_adjustments.inventory_id

4. purchases

Purpose: Records all inventory purchase transactions from suppliers.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique purchase identifier
inventory_idINTNOT NULL, FK → inventory(id)Item purchased
supplier_nameVARCHAR(255)NOT NULLName of the supplier
quantityDECIMAL(12,2)NOT NULLQuantity purchased
unit_costDECIMAL(12,2)NOT NULLCost per unit
total_costDECIMAL(14,2)NOT NULLTotal = quantity × unit_cost
purchase_dateDATENOT NULLCURRENT_DATEDate of purchase
notesTEXTNULLABLENULLAdditional notes
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPRecord creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • KEY idx_purchases_inventory_id (inventory_id)
  • KEY idx_purchases_date (purchase_date)

Foreign Keys:

  • purchases.inventory_idinventory(id) ON DELETE RESTRICT ON UPDATE CASCADE

Relationships:

  • Many-to-one with inventory
  • Triggers inventory stock adjustment on creation

5. production_runs

Purpose: Tracks manufacturing/production batch runs including input/output volumes and costs.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique production run identifier
product_nameVARCHAR(255)NOT NULLProduct being manufactured
start_dateDATENOT NULLCURRENT_DATEProduction start date
end_dateDATENULLABLENULLProduction completion date
batchesINTNOT NULL0Number of batches in this run
input_volumeDECIMAL(12,2)NOT NULL0.00Raw material volume consumed
output_volumeDECIMAL(12,2)NOT NULL0.00Finished product volume produced
input_costDECIMAL(14,2)NOT NULL0.00Total cost of input materials
output_costDECIMAL(14,2)NOT NULL0.00Total value of output produced
yield_percentDECIMAL(5,2)NOT NULL0.00Efficiency = (output / input) × 100
cost_per_litreDECIMAL(10,2)NOT NULL0.00Unit cost = output_cost / output_volume
statusENUM('Planned','In Progress','Completed','Delayed')NOT NULL'Planned'Current status of the run
notesTEXTNULLABLENULLProduction notes
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPRecord creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • KEY idx_production_status (status)
  • KEY idx_production_dates (start_date, end_date)

Foreign Keys: None

Relationships: Standalone operational table; data feeds into dashboard analytics.


6. sales

Purpose: Records all product sales transactions to customers.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique sale identifier
customer_idINTNOT NULL, FK → customers(id)Purchasing customer
inventory_idINTNOT NULL, FK → inventory(id)Product sold
quantityDECIMAL(12,2)NOT NULLQuantity sold
unit_priceDECIMAL(12,2)NOT NULLPrice per unit at time of sale
total_amountDECIMAL(14,2)NOT NULLTotal = quantity × unit_price
sale_dateDATENOT NULLCURRENT_DATEDate of sale
statusENUM('pending','completed','cancelled')NOT NULL'pending'Sale lifecycle status
notesTEXTNULLABLENULLAdditional notes
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPRecord creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • KEY idx_sales_customer_id (customer_id)
  • KEY idx_sales_inventory_id (inventory_id)
  • KEY idx_sales_date (sale_date)
  • KEY idx_sales_status (status)

Foreign Keys:

  • sales.customer_idcustomers(id) ON DELETE RESTRICT ON UPDATE CASCADE
  • sales.inventory_idinventory(id) ON DELETE RESTRICT ON UPDATE CASCADE

Relationships:

  • Many-to-one with customers
  • Many-to-one with inventory
  • One-to-many with ar_invoices

7. expenses

Purpose: Records all business expenses with approval workflow.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique expense identifier
categoryVARCHAR(100)NOT NULLExpense category (e.g., Utilities, Transport)
descriptionTEXTNOT NULLDetailed description of the expense
amountDECIMAL(14,2)NOT NULLExpense amount
payment_methodVARCHAR(50)NOT NULLPayment method (Cash, Transfer, Cheque, Card)
statusENUM('pending','approved','rejected')NOT NULL'pending'Approval status
vendorVARCHAR(255)NULLABLENULLVendor/payee name
reference_numberVARCHAR(100)NULLABLENULLTransaction reference
notesTEXTNULLABLENULLAdditional notes
expense_dateDATENOT NULLCURRENT_DATEDate the expense was incurred
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPRecord creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • KEY idx_expenses_status (status)
  • KEY idx_expenses_date (expense_date)
  • KEY idx_expenses_category (category)

Foreign Keys: None

Relationships: Feeds into accounting reports and dashboard analytics.


8. inventory_adjustments

Purpose: Logs manual adjustments to inventory stock levels with audit trail.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique adjustment identifier
inventory_idINTNOT NULL, FK → inventory(id)Item being adjusted
adjustment_quantityDECIMAL(12,2)NOT NULLChange quantity (positive = add, negative = subtract)
reasonVARCHAR(500)NOT NULLReason for the adjustment
notesTEXTNULLABLENULLDetailed notes
adjustment_dateDATENOT NULLCURRENT_DATEDate of adjustment
created_byINTNOT NULL, FK → users(id)User who made the adjustment
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPRecord creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • KEY idx_adj_inventory_id (inventory_id)
  • KEY idx_adj_created_by (created_by)
  • KEY idx_adj_date (adjustment_date)

Foreign Keys:

  • inventory_adjustments.inventory_idinventory(id) ON DELETE RESTRICT ON UPDATE CASCADE
  • inventory_adjustments.created_byusers(id) ON DELETE RESTRICT ON UPDATE CASCADE

Relationships:

  • Many-to-one with inventory
  • Many-to-one with users (audit trail)

9. ar_invoices

Purpose: Accounts Receivable invoices issued to customers for sales on credit.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique invoice identifier
sale_idINTNOT NULL, FK → sales(id)Related sale transaction
invoice_numberVARCHAR(50)NOT NULLHuman-readable invoice number
amountDECIMAL(14,2)NOT NULLInvoice amount
statusENUM('pending','paid','overdue','partial')NOT NULL'pending'Payment status
due_dateDATENOT NULLPayment due date
notesTEXTNULLABLENULLAdditional notes
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPRecord creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • UNIQUE KEY idx_ar_invoice_number (invoice_number)
  • KEY idx_ar_invoice_sale_id (sale_id)
  • KEY idx_ar_invoice_status (status)
  • KEY idx_ar_invoice_due_date (due_date)

Foreign Keys:

  • ar_invoices.sale_idsales(id) ON DELETE RESTRICT ON UPDATE CASCADE

Relationships:

  • Many-to-one with sales
  • One-to-many with ar_payments

10. ar_payments

Purpose: Records payments received against Accounts Receivable invoices.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique payment identifier
invoice_idINTNOT NULL, FK → ar_invoices(id)Related invoice
amountDECIMAL(14,2)NOT NULLPayment amount
payment_methodVARCHAR(50)NOT NULLMethod used (Cash, Transfer, Cheque, Card)
reference_numberVARCHAR(100)NULLABLENULLTransaction reference
payment_dateDATENOT NULLCURRENT_DATEDate payment was received
notesTEXTNULLABLENULLAdditional notes
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPRecord creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • KEY idx_ar_payment_invoice_id (invoice_id)
  • KEY idx_ar_payment_date (payment_date)

Foreign Keys:

  • ar_payments.invoice_idar_invoices(id) ON DELETE RESTRICT ON UPDATE CASCADE

Relationships:

  • Many-to-one with ar_invoices
  • Updates invoice status (paid/partial/overdue)

11. requisitions

Purpose: Multi-level approval workflow for purchase requisitions and expenditure requests.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique requisition identifier
titleVARCHAR(255)NOT NULLShort title of the request
descriptionTEXTNULLABLENULLDetailed description
requested_byINTNOT NULL, FK → users(id)User who initiated the request
statusENUM('pending','supervisor_approved','manager_approved','accounts_approved','md_approved','rejected')NOT NULL'pending'Current workflow status
amountDECIMAL(14,2)NULLABLENULLEstimated/requested amount
categoryVARCHAR(100)NULLABLENULLExpense category
priorityENUM('low','medium','high','urgent')NOT NULL'medium'Request priority level
supervisor_idINTNULLABLE, FK → users(id)NULLAssigned supervisor reviewer
supervisor_notesTEXTNULLABLENULLSupervisor's comments
supervisor_approved_atTIMESTAMPNULLABLENULLSupervisor approval timestamp
manager_idINTNULLABLE, FK → users(id)NULLAssigned manager reviewer
manager_notesTEXTNULLABLENULLManager's comments
manager_approved_atTIMESTAMPNULLABLENULLManager approval timestamp
accounts_idINTNULLABLE, FK → users(id)NULLAssigned accounts reviewer
accounts_notesTEXTNULLABLENULLAccounts department comments
accounts_approved_atTIMESTAMPNULLABLENULLAccounts approval timestamp
md_idINTNULLABLE, FK → users(id)NULLManaging Director reviewer
md_notesTEXTNULLABLENULLMD's comments
md_approved_atTIMESTAMPNULLABLENULLMD approval timestamp
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPRecord creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • KEY idx_req_status (status)
  • KEY idx_req_requested_by (requested_by)
  • KEY idx_req_priority (priority)
  • KEY idx_req_created_at (created_at)

Foreign Keys:

  • requisitions.requested_byusers(id) ON DELETE RESTRICT ON UPDATE CASCADE
  • requisitions.supervisor_idusers(id) ON DELETE SET NULL ON UPDATE CASCADE
  • requisitions.manager_idusers(id) ON DELETE SET NULL ON UPDATE CASCADE
  • requisitions.accounts_idusers(id) ON DELETE SET NULL ON UPDATE CASCADE
  • requisitions.md_idusers(id) ON DELETE SET NULL ON UPDATE CASCADE

Relationships:

  • Many-to-one with users (requested_by — mandatory)
  • Many-to-one with users (supervisor, manager, accounts, md — optional at each level)

Approval Workflow:

Requester creates → pending
Supervisor approves → supervisor_approved
Manager approves → manager_approved
Accounts approves → accounts_approved
MD approves → md_approved (final)
Any reviewer rejects → rejected

12. customers

Purpose: Master list of customers with credit management.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique customer identifier
company_nameVARCHAR(255)NOT NULLCompany or customer name
contact_personVARCHAR(255)NULLABLENULLPrimary contact name
phoneVARCHAR(50)NULLABLENULLPhone number
emailVARCHAR(255)NULLABLENULLEmail address
addressTEXTNULLABLENULLPhysical address
cityVARCHAR(100)NULLABLENULLCity
stateVARCHAR(100)NULLABLENULLState/region
credit_limitDECIMAL(14,2)NOT NULL0.00Maximum credit allowed
outstanding_balanceDECIMAL(14,2)NOT NULL0.00Current unpaid balance
statusENUM('Active','Inactive')NOT NULL'Active'Account status
notesTEXTNULLABLENULLAdditional notes
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPRecord creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • KEY idx_customers_status (status)
  • KEY idx_customers_company (company_name)

Foreign Keys: None (parent table)

Relationships:

  • Referenced by sales.customer_id
  • Referenced by ar_invoices (indirectly through sales)

13. suppliers

Purpose: Master list of suppliers and vendors.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique supplier identifier
company_nameVARCHAR(255)NOT NULLCompany name
contact_personVARCHAR(255)NULLABLENULLPrimary contact name
phoneVARCHAR(50)NULLABLENULLPhone number
emailVARCHAR(255)NULLABLENULLEmail address
addressTEXTNULLABLENULLPhysical address
cityVARCHAR(100)NULLABLENULLCity
stateVARCHAR(100)NULLABLENULLState/region
statusENUM('Active','Inactive')NOT NULL'Active'Account status
notesTEXTNULLABLENULLAdditional notes
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPRecord creation timestamp
updated_atTIMESTAMPNOT NULLCURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMPLast modification timestamp

Indexes:

  • PRIMARY KEY (id)
  • KEY idx_suppliers_status (status)
  • KEY idx_suppliers_company (company_name)

Foreign Keys: None (parent table)

Relationships: Referenced by name in purchases.supplier_name (no FK — denormalized for simplicity).


14. audit_log

Purpose: Immutable audit trail of all significant system actions for compliance and debugging.

ColumnTypeConstraintsDefaultDescription
idINTPRIMARY KEY, AUTO_INCREMENTUnique log entry identifier
user_idINTNOT NULL, FK → users(id)User who performed the action
actionENUM('CREATE','UPDATE','DELETE','LOGIN','APPROVE','REJECT')NOT NULLType of action performed
entity_typeVARCHAR(50)NOT NULLEntity affected (e.g., sale, invoice)
entity_idINTNULLABLENULLID of the affected record
detailsTEXTNULLABLENULLJSON or text description of the change
ip_addressVARCHAR(45)NULLABLENULLClient IP address (supports IPv6)
created_atTIMESTAMPNOT NULLCURRENT_TIMESTAMPWhen the action occurred

Indexes:

  • PRIMARY KEY (id)
  • KEY idx_audit_user_id (user_id)
  • KEY idx_audit_entity (entity_type, entity_id)
  • KEY idx_audit_action (action)
  • KEY idx_audit_created_at (created_at)

Foreign Keys:

  • audit_log.user_idusers(id) ON DELETE RESTRICT ON UPDATE CASCADE

Relationships:

  • Many-to-one with users
  • References any entity by entity_type + entity_id (polymorphic association — no FK)

Summary of Foreign Key Relationships

Source TableSource ColumnTarget TableTarget ColumnOn DeleteOn Update
purchasesinventory_idinventoryidRESTRICTCASCADE
salescustomer_idcustomersidRESTRICTCASCADE
salesinventory_idinventoryidRESTRICTCASCADE
inventory_adjustmentsinventory_idinventoryidRESTRICTCASCADE
inventory_adjustmentscreated_byusersidRESTRICTCASCADE
ar_invoicessale_idsalesidRESTRICTCASCADE
ar_paymentsinvoice_idar_invoicesidRESTRICTCASCADE
requisitionsrequested_byusersidRESTRICTCASCADE
requisitionssupervisor_idusersidSET NULLCASCADE
requisitionsmanager_idusersidSET NULLCASCADE
requisitionsaccounts_idusersidSET NULLCASCADE
requisitionsmd_idusersidSET NULLCASCADE
audit_loguser_idusersidRESTRICTCASCADE