Fix: Convert empty microchip strings to NULL in database

This commit is contained in:
2026-03-08 23:55:38 -05:00
parent 28cad68170
commit 56fb9cb7af

View File

@@ -32,6 +32,11 @@ const upload = multer({
}
});
// Helper function to convert empty strings to null
const emptyToNull = (value) => {
return (value === '' || value === undefined) ? null : value;
};
// GET all dogs
router.get('/', (req, res) => {
try {
@@ -96,10 +101,21 @@ router.post('/', (req, res) => {
const db = getDatabase();
// Convert empty strings to null for optional fields
const result = db.prepare(`
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, photo_urls)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
`).run(name, registration_number, breed, sex, birth_date, color, microchip, notes, '[]');
`).run(
name,
emptyToNull(registration_number),
breed,
sex,
emptyToNull(birth_date),
emptyToNull(color),
emptyToNull(microchip), // Convert empty string to NULL
emptyToNull(notes),
'[]'
);
const dogId = result.lastInsertRowid;
@@ -127,12 +143,23 @@ router.put('/:id', (req, res) => {
const db = getDatabase();
// Convert empty strings to null for optional fields
db.prepare(`
UPDATE dogs
SET name = ?, registration_number = ?, breed = ?, sex = ?,
birth_date = ?, color = ?, microchip = ?, notes = ?
WHERE id = ?
`).run(name, registration_number, breed, sex, birth_date, color, microchip, notes, req.params.id);
`).run(
name,
emptyToNull(registration_number),
breed,
sex,
emptyToNull(birth_date),
emptyToNull(color),
emptyToNull(microchip), // Convert empty string to NULL
emptyToNull(notes),
req.params.id
);
// Update parent relationships
db.prepare('DELETE FROM parents WHERE dog_id = ?').run(req.params.id);