AI Resource Pro / website / scripts / corrected-update-category-counts.sql
| 1 | -- SQL to update the tool_count field in the categories table |
| 2 | -- Based on the exact column names confirmed in logs and error message |
| 3 | -- CORRECTED VERSION that only uses category_id in news table, not category |
| 4 | |
| 5 | -- From logs and error message, we know the tables have these columns: |
| 6 | -- tools: id, name, slug, description, long_description, category_id, pricing, url, key_features, pros, cons, review, deployment, featured, created_at, updated_at |
| 7 | -- news: id, title, content, url, image_url, source, article_date, created_at, updated_at, category_id (NOT category) |
| 8 | -- blog_posts: has primary_category_id (based on previous logs) |
| 9 | -- categories: id, name, icon, tool_count, created_at, updated_at, slug, description |
| 10 | |
| 11 | -- Update the tool_count in the categories table |
| 12 | UPDATE categories c |
| 13 | SET tool_count = ( |
| 14 | -- Count tools with this category |
| 15 | SELECT COUNT(*) FROM tools |
| 16 | WHERE category_id = c.id |
| 17 | ) + ( |
| 18 | -- Count news with this category - using category_id only, not a category text column |
| 19 | SELECT COUNT(*) FROM news |
| 20 | WHERE category_id = c.id |
| 21 | ); |
| 22 | |
| 23 | -- Output the results for verification |
| 24 | SELECT |
| 25 | c.id, |
| 26 | c.name, |
| 27 | c.tool_count, |
| 28 | (SELECT COUNT(*) FROM tools WHERE category_id = c.id) AS tools_count, |
| 29 | (SELECT COUNT(*) FROM news WHERE category_id = c.id) AS news_count |
| 30 | FROM |
| 31 | categories c |
| 32 | ORDER BY |
| 33 | c.tool_count DESC; |