Coder Social home page Coder Social logo

Comments (4)

richardpasquiou avatar richardpasquiou commented on August 16, 2024

Did you try to set client.pragma('foreign_keys = ON'); before creating your dbClient = drizzle(client, { schema })

from drizzle-orm.

faribauc avatar faribauc commented on August 16, 2024

@richardpasquiou Wow, that was a rookie mistake 🤦 😆

Thanks for pointing that out (the error, not the "rookie" part 😃)! That being fixed, it's still not working. Here's more details:

I'm adding a migration where I add a foreign key on a table

CREATE TABLE `elevations-2` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`group` text NOT NULL,
	`description` text NOT NULL,
	`vcg` real NOT NULL,
	`is_default` integer NOT NULL,
	`file_id` integer,
	FOREIGN KEY (`file_id`) REFERENCES `file-attachments`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
INSERT INTO `elevations-2` SELECT * FROM `elevations`;--> statement-breakpoint
DROP TABLE `elevations`;

Drizzle migration tool generated the first and last SQL commands and I added the data copy in-between.

The cascading delete doesn't seem to work even when I manually delete a row from elevations in my SQL editor (dBeaver).

PRAGMA foreign_keys;
PRAGMA foreign_keys = OFF; /* to show the difference between on and off)*/
PRAGMA foreign_keys;
PRAGMA foreign_keys = ON;
PRAGMA foreign_keys;
DELETE FROM "elevations-2"  WHERE id=6;

The file-attachments entry still exists.

Is there something wrong with the new table?

from drizzle-orm.

richardpasquiou avatar richardpasquiou commented on August 16, 2024

elevations-2 is your baby table. file-attachments is your mama table. You can't drop the baby and recursively, delete the mama entry.
It works in one way : drop the mama entry, and it will cascade on baby table

from drizzle-orm.

faribauc avatar faribauc commented on August 16, 2024

@richardpasquiou Oh ok, I see. The relation field should be on file-attachments with an elevation_id field instead of having the file_id field on elevations. I was hoping not to have to do that as file-attachments links to multiple tables for a variety of entities. I guess I'll have to rethink my schema. Maybe with pivot tables... Thanks for the insight!

from drizzle-orm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.