diff --git a/src/integrations/supabase/types.ts b/src/integrations/supabase/types.ts
index 43d8629b..d02999c9 100644
--- a/src/integrations/supabase/types.ts
+++ b/src/integrations/supabase/types.ts
@@ -117,7 +117,22 @@ export type Database = {
updated_at?: string | null
view_count?: number | null
}
- Relationships: []
+ Relationships: [
+ {
+ foreignKeyName: "blog_posts_author_id_fkey"
+ columns: ["author_id"]
+ isOneToOne: false
+ referencedRelation: "filtered_profiles"
+ referencedColumns: ["user_id"]
+ },
+ {
+ foreignKeyName: "blog_posts_author_id_fkey"
+ columns: ["author_id"]
+ isOneToOne: false
+ referencedRelation: "profiles"
+ referencedColumns: ["user_id"]
+ },
+ ]
}
companies: {
Row: {
diff --git a/src/pages/AdminBlog.tsx b/src/pages/AdminBlog.tsx
index 4963e787..dedccc2a 100644
--- a/src/pages/AdminBlog.tsx
+++ b/src/pages/AdminBlog.tsx
@@ -15,6 +15,7 @@ import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from '@
import { Badge } from '@/components/ui/badge';
import { UppyPhotoUpload } from '@/components/upload/UppyPhotoUpload';
import { generateSlugFromName } from '@/lib/slugUtils';
+import { extractCloudflareImageId } from '@/lib/cloudflareImageUtils';
import { Edit, Trash2, Eye, Plus } from 'lucide-react';
import { toast } from 'sonner';
import { formatDistanceToNow } from 'date-fns';
@@ -284,11 +285,14 @@ export default function AdminBlog() {
{
- if (results.length > 0) {
- const result = results[0];
- handleImageUpload(result.cloudflareImageId, result.cloudflareImageUrl);
- toast.success('Image uploaded');
+ onUploadComplete={(urls) => {
+ if (urls.length > 0) {
+ const url = urls[0];
+ const imageId = extractCloudflareImageId(url);
+ if (imageId) {
+ handleImageUpload(imageId, url);
+ toast.success('Image uploaded');
+ }
}
}}
maxFiles={1}
diff --git a/src/pages/BlogIndex.tsx b/src/pages/BlogIndex.tsx
index 57198d0a..dd6c3f74 100644
--- a/src/pages/BlogIndex.tsx
+++ b/src/pages/BlogIndex.tsx
@@ -20,14 +20,7 @@ export default function BlogIndex() {
queryFn: async () => {
let query = supabase
.from('blog_posts')
- .select(`
- *,
- author:profiles(
- username,
- display_name,
- avatar_url
- )
- `, { count: 'exact' })
+ .select('*, profiles!inner(username, display_name, avatar_url)', { count: 'exact' })
.eq('status', 'published')
.order('published_at', { ascending: false })
.range((page - 1) * POSTS_PER_PAGE, page * POSTS_PER_PAGE - 1);
@@ -36,9 +29,9 @@ export default function BlogIndex() {
query = query.or(`title.ilike.%${searchQuery}%,content.ilike.%${searchQuery}%`);
}
- const { data, count, error } = await query;
+ const { data: posts, count, error } = await query;
if (error) throw error;
- return { posts: data, totalCount: count || 0 };
+ return { posts, totalCount: count || 0 };
},
});
@@ -92,9 +85,9 @@ export default function BlogIndex() {
content={post.content}
featuredImageId={post.featured_image_id}
author={{
- username: post.author.username,
- displayName: post.author.display_name,
- avatarUrl: post.author.avatar_url,
+ username: post.profiles.username,
+ displayName: post.profiles.display_name,
+ avatarUrl: post.profiles.avatar_url,
}}
publishedAt={post.published_at!}
viewCount={post.view_count}
diff --git a/src/pages/BlogPost.tsx b/src/pages/BlogPost.tsx
index c0024e9c..42c57131 100644
--- a/src/pages/BlogPost.tsx
+++ b/src/pages/BlogPost.tsx
@@ -18,21 +18,14 @@ export default function BlogPost() {
const { data: post, isLoading } = useQuery({
queryKey: ['blog-post', slug],
queryFn: async () => {
- const { data, error } = await supabase
+ const query = supabase
.from('blog_posts')
- .select(`
- *,
- author:profiles(
- username,
- display_name,
- avatar_url,
- avatar_image_id
- )
- `)
+ .select('*, profiles!inner(username, display_name, avatar_url, avatar_image_id)')
.eq('slug', slug)
.eq('status', 'published')
.single();
+ const { data, error } = await query;
if (error) throw error;
return data;
},
@@ -98,14 +91,14 @@ export default function BlogPost() {
-
+
- {post.author.display_name?.[0] || post.author.username[0]}
+ {post.profiles.display_name?.[0] || post.profiles.username[0]}
- {post.author.display_name || post.author.username}
+ {post.profiles.display_name || post.profiles.username}
diff --git a/supabase/migrations/20251010225807_a1fcd3b1-0bd2-49a2-acd0-ccabe9fee7e8.sql b/supabase/migrations/20251010225807_a1fcd3b1-0bd2-49a2-acd0-ccabe9fee7e8.sql
new file mode 100644
index 00000000..ee4b4488
--- /dev/null
+++ b/supabase/migrations/20251010225807_a1fcd3b1-0bd2-49a2-acd0-ccabe9fee7e8.sql
@@ -0,0 +1,4 @@
+-- Fix blog_posts foreign key to reference profiles
+ALTER TABLE blog_posts DROP CONSTRAINT IF EXISTS blog_posts_author_id_fkey;
+ALTER TABLE blog_posts ADD CONSTRAINT blog_posts_author_id_fkey
+ FOREIGN KEY (author_id) REFERENCES profiles(user_id) ON DELETE CASCADE;
\ No newline at end of file