@@ -183,20 +183,36 @@ async function seedProducts(): Promise<void> {
183183 return ;
184184 }
185185
186- // Get category IDs
187- const [ laptopRows ] = await pool . execute ( 'SELECT id FROM categories WHERE name = ?' , [ 'Laptops' ] ) ;
188- const [ booksRows ] = await pool . execute ( 'SELECT id FROM categories WHERE name = ?' , [ 'Fiction' ] ) ;
189- const [ kitchenRows ] = await pool . execute ( 'SELECT id FROM categories WHERE name = ?' , [ 'Kitchen Appliances' ] ) ;
190- const [ clothingRows ] = await pool . execute ( 'SELECT id FROM categories WHERE name = ?' , [ 'Men\'s Clothing' ] ) ;
191- const [ sportsRows ] = await pool . execute ( 'SELECT id FROM categories WHERE name = ?' , [ 'Fitness Equipment' ] ) ;
192- const [ gardenRows ] = await pool . execute ( 'SELECT id FROM categories WHERE name = ?' , [ 'Tools' ] ) ;
193-
194- const laptopCategoryId = ( laptopRows as any [ ] ) [ 0 ] ?. id ;
195- const booksCategoryId = ( booksRows as any [ ] ) [ 0 ] ?. id ;
196- const kitchenCategoryId = ( kitchenRows as any [ ] ) [ 0 ] ?. id ;
197- const clothingCategoryId = ( clothingRows as any [ ] ) [ 0 ] ?. id ;
198- const sportsCategoryId = ( sportsRows as any [ ] ) [ 0 ] ?. id ;
199- const gardenCategoryId = ( gardenRows as any [ ] ) [ 0 ] ?. id ;
186+ // Get category IDs with better error handling
187+ const getCategoryId = async ( categoryName : string ) : Promise < number | null > => {
188+ try {
189+ const [ rows ] = await pool . execute ( 'SELECT id FROM categories WHERE name = ?' , [ categoryName ] ) ;
190+ const id = ( rows as any [ ] ) [ 0 ] ?. id ;
191+ if ( ! id ) {
192+ console . warn ( `Warning: Category '${ categoryName } ' not found` ) ;
193+ }
194+ return id || null ;
195+ } catch ( error ) {
196+ console . error ( `Error finding category '${ categoryName } ':` , error ) ;
197+ return null ;
198+ }
199+ } ;
200+
201+ const laptopCategoryId = await getCategoryId ( 'Laptops' ) ;
202+ const booksCategoryId = await getCategoryId ( 'Fiction' ) ;
203+ const kitchenCategoryId = await getCategoryId ( 'Kitchen Appliances' ) ;
204+ const clothingCategoryId = await getCategoryId ( 'Men\'s Clothing' ) ;
205+ const sportsCategoryId = await getCategoryId ( 'Fitness Equipment' ) ;
206+ const gardenCategoryId = await getCategoryId ( 'Tools' ) ;
207+
208+ console . log ( 'Category IDs found:' , {
209+ laptopCategoryId,
210+ booksCategoryId,
211+ kitchenCategoryId,
212+ clothingCategoryId,
213+ sportsCategoryId,
214+ gardenCategoryId
215+ } ) ;
200216
201217 const products = [
202218 {
@@ -265,9 +281,12 @@ async function seedProducts(): Promise<void> {
265281 product . inventory_quantity
266282 ]
267283 ) ;
284+ console . log ( `✓ Inserted product: ${ product . name } (category_id: ${ product . category_id } )` ) ;
268285 } catch ( error ) {
269- console . warn ( `Warning: Could not insert product ${ product . name } :` , error ) ;
286+ console . warn ( `✗ Could not insert product ${ product . name } :` , error ) ;
270287 }
288+ } else {
289+ console . warn ( `✗ Skipping product ${ product . name } - no category_id` ) ;
271290 }
272291 }
273292
0 commit comments