Fix: Remove weight/height columns - match actual schema
This commit is contained in:
@@ -41,10 +41,10 @@ const emptyToNull = (value) => {
|
|||||||
router.get('/', (req, res) => {
|
router.get('/', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
// Select only the fields we want, excluding sire/dam if they exist
|
// Select only fields that exist in the schema (no weight/height)
|
||||||
const dogs = db.prepare(`
|
const dogs = db.prepare(`
|
||||||
SELECT id, name, registration_number, microchip, sex, birth_date, breed,
|
SELECT id, name, registration_number, breed, sex, birth_date,
|
||||||
color, weight, height, notes, litter_id, photo_urls, is_active,
|
color, microchip, photo_urls, notes, is_active,
|
||||||
created_at, updated_at
|
created_at, updated_at
|
||||||
FROM dogs
|
FROM dogs
|
||||||
WHERE is_active = 1
|
WHERE is_active = 1
|
||||||
@@ -66,10 +66,10 @@ router.get('/', (req, res) => {
|
|||||||
router.get('/:id', (req, res) => {
|
router.get('/:id', (req, res) => {
|
||||||
try {
|
try {
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
// Select only the fields we want, excluding sire/dam if they exist
|
// Select only fields that exist in the schema (no weight/height)
|
||||||
const dog = db.prepare(`
|
const dog = db.prepare(`
|
||||||
SELECT id, name, registration_number, microchip, sex, birth_date, breed,
|
SELECT id, name, registration_number, breed, sex, birth_date,
|
||||||
color, weight, height, notes, litter_id, photo_urls, is_active,
|
color, microchip, photo_urls, notes, is_active,
|
||||||
created_at, updated_at
|
created_at, updated_at
|
||||||
FROM dogs
|
FROM dogs
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
@@ -116,22 +116,49 @@ router.post('/', (req, res) => {
|
|||||||
|
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
|
|
||||||
// Convert empty strings to null for optional fields
|
// Check if litter_id column exists
|
||||||
const result = db.prepare(`
|
let hasLitterId = false;
|
||||||
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, litter_id, photo_urls)
|
try {
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
const columns = db.prepare("PRAGMA table_info(dogs)").all();
|
||||||
`).run(
|
hasLitterId = columns.some(col => col.name === 'litter_id');
|
||||||
name,
|
} catch (e) {
|
||||||
emptyToNull(registration_number),
|
console.error('Error checking schema:', e);
|
||||||
breed,
|
}
|
||||||
sex,
|
|
||||||
emptyToNull(birth_date),
|
// Insert with or without litter_id depending on schema
|
||||||
emptyToNull(color),
|
let result;
|
||||||
emptyToNull(microchip),
|
if (hasLitterId) {
|
||||||
emptyToNull(notes),
|
result = db.prepare(`
|
||||||
emptyToNull(litter_id),
|
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, litter_id, photo_urls)
|
||||||
'[]'
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
);
|
`).run(
|
||||||
|
name,
|
||||||
|
emptyToNull(registration_number),
|
||||||
|
breed,
|
||||||
|
sex,
|
||||||
|
emptyToNull(birth_date),
|
||||||
|
emptyToNull(color),
|
||||||
|
emptyToNull(microchip),
|
||||||
|
emptyToNull(notes),
|
||||||
|
emptyToNull(litter_id),
|
||||||
|
'[]'
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
result = db.prepare(`
|
||||||
|
INSERT INTO dogs (name, registration_number, breed, sex, birth_date, color, microchip, notes, photo_urls)
|
||||||
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
|
`).run(
|
||||||
|
name,
|
||||||
|
emptyToNull(registration_number),
|
||||||
|
breed,
|
||||||
|
sex,
|
||||||
|
emptyToNull(birth_date),
|
||||||
|
emptyToNull(color),
|
||||||
|
emptyToNull(microchip),
|
||||||
|
emptyToNull(notes),
|
||||||
|
'[]'
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
const dogId = result.lastInsertRowid;
|
const dogId = result.lastInsertRowid;
|
||||||
|
|
||||||
@@ -144,8 +171,8 @@ router.post('/', (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const dog = db.prepare(`
|
const dog = db.prepare(`
|
||||||
SELECT id, name, registration_number, microchip, sex, birth_date, breed,
|
SELECT id, name, registration_number, breed, sex, birth_date,
|
||||||
color, weight, height, notes, litter_id, photo_urls, is_active,
|
color, microchip, photo_urls, notes, is_active,
|
||||||
created_at, updated_at
|
created_at, updated_at
|
||||||
FROM dogs
|
FROM dogs
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
@@ -165,24 +192,52 @@ router.put('/:id', (req, res) => {
|
|||||||
|
|
||||||
const db = getDatabase();
|
const db = getDatabase();
|
||||||
|
|
||||||
// Convert empty strings to null for optional fields
|
// Check if litter_id column exists
|
||||||
db.prepare(`
|
let hasLitterId = false;
|
||||||
UPDATE dogs
|
try {
|
||||||
SET name = ?, registration_number = ?, breed = ?, sex = ?,
|
const columns = db.prepare("PRAGMA table_info(dogs)").all();
|
||||||
birth_date = ?, color = ?, microchip = ?, notes = ?, litter_id = ?
|
hasLitterId = columns.some(col => col.name === 'litter_id');
|
||||||
WHERE id = ?
|
} catch (e) {
|
||||||
`).run(
|
console.error('Error checking schema:', e);
|
||||||
name,
|
}
|
||||||
emptyToNull(registration_number),
|
|
||||||
breed,
|
// Update with or without litter_id
|
||||||
sex,
|
if (hasLitterId) {
|
||||||
emptyToNull(birth_date),
|
db.prepare(`
|
||||||
emptyToNull(color),
|
UPDATE dogs
|
||||||
emptyToNull(microchip),
|
SET name = ?, registration_number = ?, breed = ?, sex = ?,
|
||||||
emptyToNull(notes),
|
birth_date = ?, color = ?, microchip = ?, notes = ?, litter_id = ?
|
||||||
emptyToNull(litter_id),
|
WHERE id = ?
|
||||||
req.params.id
|
`).run(
|
||||||
);
|
name,
|
||||||
|
emptyToNull(registration_number),
|
||||||
|
breed,
|
||||||
|
sex,
|
||||||
|
emptyToNull(birth_date),
|
||||||
|
emptyToNull(color),
|
||||||
|
emptyToNull(microchip),
|
||||||
|
emptyToNull(notes),
|
||||||
|
emptyToNull(litter_id),
|
||||||
|
req.params.id
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
db.prepare(`
|
||||||
|
UPDATE dogs
|
||||||
|
SET name = ?, registration_number = ?, breed = ?, sex = ?,
|
||||||
|
birth_date = ?, color = ?, microchip = ?, notes = ?
|
||||||
|
WHERE id = ?
|
||||||
|
`).run(
|
||||||
|
name,
|
||||||
|
emptyToNull(registration_number),
|
||||||
|
breed,
|
||||||
|
sex,
|
||||||
|
emptyToNull(birth_date),
|
||||||
|
emptyToNull(color),
|
||||||
|
emptyToNull(microchip),
|
||||||
|
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);
|
||||||
@@ -195,8 +250,8 @@ router.put('/:id', (req, res) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const dog = db.prepare(`
|
const dog = db.prepare(`
|
||||||
SELECT id, name, registration_number, microchip, sex, birth_date, breed,
|
SELECT id, name, registration_number, breed, sex, birth_date,
|
||||||
color, weight, height, notes, litter_id, photo_urls, is_active,
|
color, microchip, photo_urls, notes, is_active,
|
||||||
created_at, updated_at
|
created_at, updated_at
|
||||||
FROM dogs
|
FROM dogs
|
||||||
WHERE id = ?
|
WHERE id = ?
|
||||||
|
|||||||
Reference in New Issue
Block a user