Skip to content
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

fix(demoSeed): fixed false position of default license #123

Closed
wants to merge 5 commits into from

Conversation

lukas-png
Copy link
Contributor

@lukas-png lukas-png commented Feb 14, 2024

Title: Fix Foreign Key Constraint Error by Ensuring Default License ID

Description:

This pull request addresses a critical issue where our seeding function encounters a foreign key constraint error due to the absence of a default license with ID 0. Our seeding logic mandates the existence of a default license, specifically identified by ID 0. This default license is crucial for the seeding process, as it is automatically assigned to newly created lessons where no specific license ID is provided.

Previously, the lack of a default license with the expected ID 0 in the database led to foreign key constraint violations, disrupting the seeding process. This issue was identified during the execution of our seeding function, which explicitly refers to the default license ID 0 for lessons that do not have a specific license assigned.

@lukas-png lukas-png linked an issue Feb 14, 2024 that may be closed by this pull request
@lukas-png lukas-png requested a review from spark-sse February 14, 2024 21:48
@spark-sse spark-sse force-pushed the fix/122-seeding-throws-error branch from d0427fc to c19f2e2 Compare February 15, 2024 13:29
Copy link
Member

@spark-sse spark-sse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kann man das anders hinkriegen? Prisma kümmert sich um die auto-increments automatisch sobald man keine IDs anlegt. Sobald hier eine spezifiziert wird, gibt es das Problem, dass in einer neuen Installation beim ersten mal keine Lizenz angelegt werden kann.

Idee ist, dass anders auf die Default License referenziert wird.

@spark-sse spark-sse force-pushed the master branch 2 times, most recently from 1cfc8d4 to da05d2a Compare February 15, 2024 20:13
@spark-sse spark-sse force-pushed the fix/122-seeding-throws-error branch from c19f2e2 to 7af30a1 Compare February 15, 2024 20:25
@lukas-png
Copy link
Contributor Author

Kann man das anders hinkriegen? Prisma kümmert sich um die auto-increments automatisch sobald man keine IDs anlegt. Sobald hier eine spezifiziert wird, gibt es das Problem, dass in einer neuen Installation beim ersten mal keine Lizenz angelegt werden kann.

Idee ist, dass anders auf die Default License referenziert wird.

ich könnte die id der default license nach dem erstellen zwischen speichern.

@spark-sse
Copy link
Member

Das wäre eine option.

Wo wird die Lizenz benötigt? Kannst du die Code-Stelle einmal verlinken?

@lukas-png
Copy link
Contributor Author

Das wäre eine option.

Wo wird die Lizenz benötigt? Kannst du die Code-Stelle einmal verlinken?

In seed-functions -> seedCaseStudy

	const courseData: Prisma.CourseCreateManyInput[] = courses.map(c => c.data);
	await prisma.course.createMany({ data: courseData });
	console.log(" - %s\x1b[32m ✔\x1b[0m", "Courses");

	await prisma.lesson.createMany({
		data: chapters.flatMap(chapter =>
			chapter.content.map(lesson => ({
				...lesson,
				licenseId: lesson.licenseId ?? 0
			}))
		)
	});
	console.log(" - %s\x1b[32m ✔\x1b[0m", "Lessons");

Die 0 ist die genaue Stelle

@spark-sse
Copy link
Member

Alles klar. Ist es sehr aufwendig die defaultLicense in einer konstanten zu speichern und zu exportieren? Die würde dann den Chaptern entsprechend zugewiesen werden. So braucht mann dann in den seedFunctions den map nicht mehr.

@lukas-png
Copy link
Contributor Author

Alles klar. Ist es sehr aufwendig die defaultLicense in einer konstanten zu speichern und zu exportieren? Die würde dann den Chaptern entsprechend zugewiesen werden. So braucht mann dann in den seedFunctions den map nicht mehr.

Du meint dann als globale bzw Module scoped variable ? Dann muss man halt die Default license aus dem create many rausnehmen und einzeln am Anfang hinzufügen

@spark-sse
Copy link
Member

module scope

@lukas-png lukas-png force-pushed the fix/122-seeding-throws-error branch from 94765ea to 457096b Compare March 21, 2024 12:36
@lukas-png lukas-png force-pushed the fix/122-seeding-throws-error branch from 457096b to 55834f4 Compare March 21, 2024 12:37
@@ -535,9 +536,13 @@ const completedLessons: Prisma.CompletedLessonCreateManyInput[] = extractLessonI
createdAt: subHours(Date.now(), index * 4)
}));


export let defaultLicenseID = 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mutable export variables sind quasi globals

Comment on lines +276 to +278
const module = await import ("./demo/demo");
const defaultLicense = module.defaultLicenseID;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

können wir das irgendwie ohne den lazy import hinkriegen?

…ility' into fix/122-seeding-throws-error

# Conflicts:
#	libs/data-access/database/src/lib/demo/demo.ts
#	libs/data-access/database/src/lib/seed-functions.ts
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.

Seeding throws error
3 participants