Fix: Ensure sire_id and dam_id are sent as null when empty, not empty strings

This commit is contained in:
2026-03-09 01:16:51 -05:00
parent 39013fc7c5
commit 485cc15a3e

View File

@@ -12,9 +12,9 @@ function DogForm({ dog, onClose, onSave }) {
color: '',
microchip: '',
notes: '',
sire_id: '',
dam_id: '',
litter_id: ''
sire_id: null, // Changed from '' to null
dam_id: null, // Changed from '' to null
litter_id: null // Changed from '' to null
})
const [dogs, setDogs] = useState([])
const [litters, setLitters] = useState([])
@@ -36,9 +36,9 @@ function DogForm({ dog, onClose, onSave }) {
color: dog.color || '',
microchip: dog.microchip || '',
notes: dog.notes || '',
sire_id: dog.sire?.id || '',
dam_id: dog.dam?.id || '',
litter_id: dog.litter_id || ''
sire_id: dog.sire?.id || null, // Ensure null, not ''
dam_id: dog.dam?.id || null, // Ensure null, not ''
litter_id: dog.litter_id || null // Ensure null, not ''
})
setUseManualParents(!dog.litter_id)
}
@@ -75,7 +75,14 @@ function DogForm({ dog, onClose, onSave }) {
const handleChange = (e) => {
const { name, value } = e.target
setFormData(prev => ({ ...prev, [name]: value }))
// Convert empty strings to null for ID fields
let processedValue = value
if (name === 'sire_id' || name === 'dam_id' || name === 'litter_id') {
processedValue = value === '' ? null : parseInt(value)
}
setFormData(prev => ({ ...prev, [name]: processedValue }))
// If litter is selected, auto-populate parents
if (name === 'litter_id' && value) {
@@ -97,11 +104,17 @@ function DogForm({ dog, onClose, onSave }) {
setLoading(true)
try {
const submitData = { ...formData }
// Clear litter_id if using manual parent selection
if (useManualParents) {
submitData.litter_id = null
const submitData = {
...formData,
// Ensure null values are sent, not empty strings
sire_id: formData.sire_id || null,
dam_id: formData.dam_id || null,
litter_id: useManualParents ? null : (formData.litter_id || null),
registration_number: formData.registration_number || null,
birth_date: formData.birth_date || null,
color: formData.color || null,
microchip: formData.microchip || null,
notes: formData.notes || null
}
if (dog) {
@@ -254,7 +267,7 @@ function DogForm({ dog, onClose, onSave }) {
<select
name="litter_id"
className="input"
value={formData.litter_id}
value={formData.litter_id || ''}
onChange={handleChange}
>
<option value="">No Litter</option>
@@ -277,7 +290,7 @@ function DogForm({ dog, onClose, onSave }) {
<select
name="sire_id"
className="input"
value={formData.sire_id}
value={formData.sire_id || ''}
onChange={handleChange}
>
<option value="">Unknown</option>
@@ -292,7 +305,7 @@ function DogForm({ dog, onClose, onSave }) {
<select
name="dam_id"
className="input"
value={formData.dam_id}
value={formData.dam_id || ''}
onChange={handleChange}
>
<option value="">Unknown</option>