Skip to content

[Feature Request]: Database schema consistency: Primary Key/Foreign Key type mismatch #1225

@iabok

Description

@iabok

Preflight Checklist

Problem Description

@dmohns This is Isaac from Ignite Energy Access: Consider the following DDL.

CREATE TABLE `meter_types` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `online` tinyint(1) NOT NULL DEFAULT 0,
  `phase` int(11) NOT NULL DEFAULT 1,
  `max_current` int(11) NOT NULL DEFAULT 10,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
)

CREATE TABLE `meters` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `serial_number` varchar(191) NOT NULL,
  `meter_type_id` int(11) NOT NULL,
  `in_use` tinyint(1) NOT NULL DEFAULT 0,
  `manufacturer_id` int(11) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `connection_type_id` int(11) NOT NULL,
  `connection_group_id` int(11) NOT NULL,
  `tariff_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `meters_serial_number_unique` (`serial_number`)
)

CREATE TABLE `access_rate_payments` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `meter_id` int(11) NOT NULL,
  `access_rate_id` int(11) NOT NULL,
  `due_date` datetime NOT NULL,
  `debt` double NOT NULL DEFAULT 0,
  `unpaid_in_row` double NOT NULL DEFAULT 0,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
)

CREATE TABLE `meter_tariffs` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(191) NOT NULL,
  `price` double NOT NULL,
  `currency` varchar(20) NOT NULL,
  `factor` int(11) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `total_price` double DEFAULT NULL,
  `minimum_purchase_amount` double NOT NULL DEFAULT 0,
  PRIMARY KEY (`id`)
)

Most primary keys are INT(10) UNSIGNED, while foreign-key-like columns (e.g., meter_type_id, manufacturer_id, tariff_id, meter_id, access_rate_id) to name but a few are INT(11) signed. See the schema above. These should match exactly (size and unsignedness).

If you add foreign keys later, MySQL will require matching definitions; So, as-is, you can even store negative IDs in foreign keys, which is logically invalid.

Proposed Solution

N/A

Alternatives Considered

N/A

Additional Information

N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions