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