Skip to content

fix: many to many table name #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

SukeshP1995
Copy link

@SukeshP1995 SukeshP1995 commented Sep 16, 2024

The name for manyToManyTable should be mapped to manyTableName otherwise this won't work. If you use the below schema.

generator client {
  provider      = "prisma-client-js"
  binaryTargets = ["native", "debian-openssl-1.1.x"]
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
} 

model Post {
  id         Int        @id @default(autoincrement())
  title      String
  categories Category[] @relation("PC", map: "PC")
}

model Category {
  id    Int    @id @default(autoincrement())
  name  String
  posts Post[] @relation("PC", map: "PC")
}

without fix

import { relations } from 'drizzle-orm'
import { foreignKey, integer, pgTable, serial, text } from 'drizzle-orm/pg-core'

export const Post = pgTable('Post', {
	id: serial('id').notNull().primaryKey(),
	title: text('title').notNull()
});

export const Category = pgTable('Category', {
	id: serial('id').notNull().primaryKey(),
	name: text('name').notNull()
});

export const PC = pgTable('_PC', {
	PostId: integer('A').notNull(),
	CategoryId: integer('B').notNull()
}, (PC) => ({
	'_PC_Post_fkey': foreignKey({
		name: '_PC_Post_fkey',
		columns: [PC.PostId],
		foreignColumns: [Post.id]
	})
		.onDelete('cascade')
		.onUpdate('cascade'),
	'_PC_Category_fkey': foreignKey({
		name: '_PC_Category_fkey',
		columns: [PC.CategoryId],
		foreignColumns: [Category.id]
	})
		.onDelete('cascade')
		.onUpdate('cascade')
}));

export const PostRelations = relations(Post, ({ many }) => ({
	categories: many(CategoryToPost, {
		relationName: 'PostToCategoryToPost'
	})
}));

export const CategoryRelations = relations(Category, ({ many }) => ({
	posts: many(CategoryToPost, {
		relationName: 'CategoryToCategoryToPost'
	})
}));

export const PCRelations = relations(PC, ({ one }) => ({
	Post: one(Post, {
		relationName: 'PostToCategoryToPost',
		fields: [PC.PostId],
		references: [Post.id]
	}),
	Category: one(Category, {
		relationName: 'CategoryToCategoryToPost',
		fields: [PC.CategoryId],
		references: [Category.id]
	})
}));

with fix:

import { relations } from 'drizzle-orm'
import { foreignKey, integer, pgTable, serial, text } from 'drizzle-orm/pg-core'

export const Post = pgTable('Post', {
	id: serial('id').notNull().primaryKey(),
	title: text('title').notNull()
});

export const Category = pgTable('Category', {
	id: serial('id').notNull().primaryKey(),
	name: text('name').notNull()
});

export const PostToCategory = pgTable('_PC', {
	PostId: integer('A').notNull(),
	CategoryId: integer('B').notNull()
}, (PostToCategory) => ({
	'_PC_Post_fkey': foreignKey({
		name: '_PC_Post_fkey',
		columns: [PostToCategory.PostId],
		foreignColumns: [Post.id]
	})
		.onDelete('cascade')
		.onUpdate('cascade'),
	'_PC_Category_fkey': foreignKey({
		name: '_PC_Category_fkey',
		columns: [PostToCategory.CategoryId],
		foreignColumns: [Category.id]
	})
		.onDelete('cascade')
		.onUpdate('cascade')
}));

export const PostRelations = relations(Post, ({ many }) => ({
	categories: many(PostToCategory, {
		relationName: 'PostToCategoryToPost'
	})
}));

export const CategoryRelations = relations(Category, ({ many }) => ({
	posts: many(PostToCategory, {
		relationName: 'CategoryToCategoryToPost'
	})
}));

export const PostToCategoryRelations = relations(PostToCategory, ({ one }) => ({
	Post: one(Post, {
		relationName: 'PostToCategoryToPost',
		fields: [PostToCategory.PostId],
		references: [Post.id]
	}),
	Category: one(Category, {
		relationName: 'CategoryToCategoryToPost',
		fields: [PostToCategory.CategoryId],
		references: [Category.id]
	})
}));

@Sukairo-02 Sukairo-02 self-assigned this Oct 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants