Fix: Convert empty microchip strings to NULL in database
This commit is contained in:
@@ -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
|
// GET all dogs
|
||||||
router.get('/', (req, res) => {
|
router.get('/', (req, res) => {
|
||||||
try {
|
try {
|
||||||
@@ -96,10 +101,21 @@ router.post('/', (req, res) => {
|
|||||||
|
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
|
|
||||||
|
// Convert empty strings to null for optional fields
|
||||||
const result = db.prepare(`
|
const result = db.prepare(`
|
||||||
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, photo_urls)
|
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, photo_urls)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
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;
|
const dogId = result.lastInsertRowid;
|
||||||
|
|
||||||
@@ -127,12 +143,23 @@ router.put('/:id', (req, res) => {
|
|||||||
|
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
|
|
||||||
|
// Convert empty strings to null for optional fields
|
||||||
db.prepare(`
|
db.prepare(`
|
||||||
UPDATE dogs
|
UPDATE dogs
|
||||||
SET name = ?, registration_number = ?, breed = ?, sex = ?,
|
SET name = ?, registration_number = ?, breed = ?, sex = ?,
|
||||||
birth_date = ?, color = ?, microchip = ?, notes = ?
|
birth_date = ?, color = ?, microchip = ?, notes = ?
|
||||||
WHERE id = ?
|
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
|
// Update parent relationships
|
||||||
db.prepare('DELETE FROM parents WHERE dog_id = ?').run(req.params.id);
|
db.prepare('DELETE FROM parents WHERE dog_id = ?').run(req.params.id);
|
||||||
|
|||||||
Reference in New Issue
Block a user