Skip to content

Add database schema supporting multiple water meters per consumer#2

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/update-database-schema
Draft

Add database schema supporting multiple water meters per consumer#2
Copilot wants to merge 4 commits intomainfrom
copilot/update-database-schema

Conversation

Copy link

Copilot AI commented Dec 15, 2025

Current schema ties readings and images directly to consumers, preventing scenarios where one consumer has multiple water meters at different locations.

Schema Changes

consumers table

  • Added customer_code VARCHAR(50) UNIQUE NOT NULL for external customer identification

water_meters table (new)

  • Links consumers to their meters (1:N relationship)
  • meter_code VARCHAR(50) UNIQUE NOT NULL - unique meter identifier
  • location VARCHAR(255) - physical location of meter
  • is_active BOOLEAN - supports meter lifecycle management
  • Foreign key: consumer_id → consumers(id)

images and readings tables

  • Changed foreign key from consumer_id to water_meter_id
  • Links readings/images to specific meters instead of consumers

Sample Data

-- Consumer K-001 has two meters at different locations
INSERT INTO water_meters (consumer_id, meter_code, location, installation_date) VALUES
    ((SELECT id FROM consumers WHERE customer_code = 'K-001'), 'VM-001-A', 'Glavni vodomer', '2024-01-15'),
    ((SELECT id FROM consumers WHERE customer_code = 'K-001'), 'VM-001-B', 'Vodomer u bašti', '2024-01-15');

Future Work

Application code (database.py, main.py) needs updates to use water_meter_id instead of consumer_id.

Original prompt

Problem

Trenutna struktura baze podataka ne podržava sledeće scenarije:

  • Jedan korisnik može imati više vodomera koje treba očitati
  • Nedostaje šifra korisnika (customer_code) za jedinstvenu identifikaciju
  • Očitavanja i slike su direktno vezane za korisnika umesto za konkretan vodomer

Rešenje

Potrebno je ažurirati init.sql fajl sa sledećim promenama:

1. Ažurirati consumers tabelu

Dodati kolonu customer_code za jedinstvenu šifru korisnika:

CREATE TABLE IF NOT EXISTS consumers (
    id INT PRIMARY KEY AUTO_INCREMENT,
    customer_code VARCHAR(50) UNIQUE NOT NULL,
    name VARCHAR(255) NOT NULL,
    address VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

2. Kreirati novu tabelu water_meters

Dodati novu tabelu koja omogućava da jedan korisnik ima više vodomera:

CREATE TABLE IF NOT EXISTS water_meters (
    id INT PRIMARY KEY AUTO_INCREMENT,
    consumer_id INT NOT NULL,
    meter_code VARCHAR(50) UNIQUE NOT NULL,
    location VARCHAR(255),
    installation_date DATE,
    is_active TINYINT DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (consumer_id) REFERENCES consumers(id)
);

3. Ažurirati images tabelu

Promeniti foreign key sa consumer_id na water_meter_id:

CREATE TABLE IF NOT EXISTS images (
    id INT PRIMARY KEY AUTO_INCREMENT,
    water_meter_id INT NOT NULL,
    image_url VARCHAR(512) NOT NULL,
    processed TINYINT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (water_meter_id) REFERENCES water_meters(id)
);

4. Ažurirati readings tabelu

Promeniti foreign key sa consumer_id na water_meter_id:

CREATE TABLE IF NOT EXISTS readings (
    id INT PRIMARY KEY AUTO_INCREMENT,
    water_meter_id INT NOT NULL,
    reading_value DECIMAL(10,2),
    image_path VARCHAR(512),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (water_meter_id) REFERENCES water_meters(id)
);

5. Ažurirati test podatke

Primer podataka koji demonstrira scenario gde jedan korisnik ima više vodomera:

-- Insert sample consumers
INSERT INTO consumers (customer_code, name, address) VALUES 
    ('K-001', 'Petar Petrović', 'Kralja Petra 1, Beograd'),
    ('K-002', 'Marija Marković', 'Bulevar oslobođenja 23, Novi Sad'),
    ('K-003', 'Jovan Jovanović', 'Kneza Miloša 45, Niš');

-- Insert sample water meters (showing consumer K-001 has 2 meters)
INSERT INTO water_meters (consumer_id, meter_code, location, installation_date) VALUES
    (1, 'VM-001-A', 'Glavni vodomer', '2024-01-15'),
    (1, 'VM-001-B', 'Vodomer u bašti', '2024-01-15'),
    (2, 'VM-002-A', 'Stan', '2024-02-10'),
    (3, 'VM-003-A', 'Kuća', '2024-03-05');

Benefiti nove strukture

✅ Jedan korisnik može imati više vodomera
✅ Svaki vodomer ima svoju jedinstvenu šifru (meter_code)
✅ Očitavanja su vezana za konkretan vodomer
✅ Lakše praćenje lokacije svakog vodomera
✅ Mogućnost deaktivacije starih vodomera (is_active flag)
✅ Jedinstvena šifra korisnika (customer_code)

Napomena

Ove promene menjaju samo init.sql fajl. Biće potrebno ažurirati i:

  • database.py - dodati funkcije za rad sa water_meters tabelom
  • main.py - ažurirati endpoint da prima water_meter_id umesto consumer_id

Ali to može biti u sledećem PR-u nakon što se ova struktura odobri.

This pull request was created as a result of the following prompt from Copilot chat.

Problem

Trenutna struktura baze podataka ne podržava sledeće scenarije:

  • Jedan korisnik može imati više vodomera koje treba očitati
  • Nedostaje šifra korisnika (customer_code) za jedinstvenu identifikaciju
  • Očitavanja i slike su direktno vezane za korisnika umesto za konkretan vodomer

Rešenje

Potrebno je ažurirati init.sql fajl sa sledećim promenama:

1. Ažurirati consumers tabelu

Dodati kolonu customer_code za jedinstvenu šifru korisnika:

CREATE TABLE IF NOT EXISTS consumers (
    id INT PRIMARY KEY AUTO_INCREMENT,
    customer_code VARCHAR(50) UNIQUE NOT NULL,
    name VARCHAR(255) NOT NULL,
    address VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

2. Kreirati novu tabelu water_meters

Dodati novu tabelu koja omogućava da jedan korisnik ima više vodomera:

CREATE TABLE IF NOT EXISTS water_meters (
    id INT PRIMARY KEY AUTO_INCREMENT,
    consumer_id INT NOT NULL,
    meter_code VARCHAR(50) UNIQUE NOT NULL,
    location VARCHAR(255),
    installation_date DATE,
    is_active TINYINT DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (consumer_id) REFERENCES consumers(id)
);

3. Ažurirati images tabelu

Promeniti foreign key sa consumer_id na water_meter_id:

CREATE TABLE IF NOT EXISTS images (
    id INT PRIMARY KEY AUTO_INCREMENT,
    water_meter_id INT NOT NULL,
    image_url VARCHAR(512) NOT NULL,
    processed TINYINT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (water_meter_id) REFERENCES water_meters(id)
);

4. Ažurirati readings tabelu

Promeniti foreign key sa consumer_id na water_meter_id:

CREATE TABLE IF NOT EXISTS readings (
    id INT PRIMARY KEY AUTO_INCREMENT,
    water_meter_id INT NOT NULL,
    reading_value DECIMAL(10,2),
    image_path VARCHAR(512),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (water_meter_id) REFERENCES water_meters(id)
);

5. Ažurirati test podatke

Primer podataka koji demonstrira scenario gde jedan korisnik ima više vodomera:

-- Insert sample consumers
INSERT INTO consumers (customer_code, name, address) VALUES 
    ('K-001', 'Petar Petrović', 'Kralja Petra 1, Beograd'),
    ('K-002', 'Marija Marković', 'Bulevar oslobođenja 23, Novi Sad'),
    ('K-003', 'Jovan Jovanović', 'Kneza Miloša 45, Niš');

-- Insert sample water meters (showing consumer K-001 has 2 meters)
INSERT INTO water_meters (consumer_id, meter_code, location, installation_date) VALUES
    (1, 'VM-001-A', 'Glavni vodomer', '2024-01-15'),
    (1, 'VM-001-B', 'Vodomer u bašti', '2024-01-15'),
    (2, 'VM-002-A', 'Stan', '2024-02-10'),
    (3, 'VM-003-A', 'Kuća', '2024-03-05');

Benefiti nove strukture

✅ Jedan korisnik može imati više vodomera
✅ Svaki vodomer ima svoju jedinstvenu šifru (meter_code)
✅ Očitavanja su vezana za konkretan vodomer
✅ Lakše praćenje lokacije svakog vodomera
✅ Mogućnost deaktivacije starih vodomera (is_active flag)
✅ Jedinstvena šifra korisnika (customer_code)

Napomena

Ove promene menjaju samo init.sql fajl. Biće potrebno ažurirati i:

  • database.py - dodati funkcije za rad sa water_meters tabelom
  • main.py - ažurirati endpoint da prima water_meter_id umesto consumer_id

Ali to može biti u sledećem PR-u nakon što se ova struktura odobri.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 3 commits December 15, 2025 23:37
…eters per consumer

Co-authored-by: drdule <204492158+drdule@users.noreply.github.com>
…ata insertion

Co-authored-by: drdule <204492158+drdule@users.noreply.github.com>
Co-authored-by: drdule <204492158+drdule@users.noreply.github.com>
Copilot AI changed the title [WIP] Update init.sql for consumers and water_meters tables Add database schema supporting multiple water meters per consumer Dec 15, 2025
Copilot AI requested a review from drdule December 15, 2025 23:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants