EnAccess / EnAccess/micropowermanager

[Feature Request]: Foreign Key types should match Primary Key types (to avoid Database schema inconsistency)

Open
#1,225 4 comments 0 reactions 0 assignees View on GitHub
enhancement good first issue help wanted
Dominant language
PHP
Stars
27
Forks
19
Avg merge
1d 18h
Merged PRs (30d)
27

Description

### Preflight Checklist

- [x] I have read the [Contributing Guidelines](https://github.com/EnAccess/micropowermanager/blob/main/CONTRIBUTING.md) for this project, if it exists.
- [x] I agree to follow the [Code of Conduct](https://github.com/EnAccess/micropowermanager/blob/main/CODE_OF_CONDUCT.md) that this project adheres to.
- [x] I have searched the [issue tracker](https://github.com/EnAccess/micropowermanager/issues) for a feature request that matches the one I want to file, without success.

### 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

(based on [discussion in the issue](https://github.com/EnAccess/micropowermanager/issues/1225#issuecomment-3718188495))

Please implement the following steps to address the database inconsistencies w.r.t to primary and foreign keys.

- Create all Primary Keys using [`id()`](https://laravel.com/docs/12.x/migrations#column-method-id) (deprecate usage of `bigIncrements` and `bigIncrements` which created `int`)
- Foreign Key columns should be created using [`foreignId(...)`](https://laravel.com/docs/12.x/migrations#column-method-foreignId) and [`morphs(...)`](https://laravel.com/docs/12.x/migrations#column-method-morphs).

This should ensure consistencies with columns created with `id()` and make it easy to add indexes and constraints later using [`constrained()`](https://laravel.com/docs/12.x/migrations#foreign-key-constraints)

### Alternatives Considered

N/A

### Additional Information

N/A

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.