[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] Re: Questions on using orm
Hi, Two answers to your questions: === Answer 1 (the direct answer) === > Question 1: is there any way to actually use foreign keys to query? > > I would like to do basically: > > SELECT c.* from Chapter c, Book b where b.book_name = "My book"; You can use custom functions: let get_chapters_by_book_name db book_name = chapter_get db ~custom:(fun c -> c.chapter_fk_book.book_name = book_name) > --- > Question 2: is there any way to do cascading deletes? > > chapter_delete ~recursive:true ~db:chapter_db some_chapter > > This does something I cannot really envision ever needing...It allows me to > delete the chapter _and_ the book (doesn't really seem like something too > natural), where as I would like the reverse to actually happen: > > book_delete ~recursive:true ~db:book_db some_book > > I would expect this to delete the book and everything that actually keeps > foreign keys to the book item, much like cascading deletes in SQL. > > Instead, at the moment, I have to query for the book object, then use it to > query for all chapters underneath, use each to call chapter_delete > individually, and afterwards delete the book. There is something here that > feels it won't just be optimized away for me :) This is not possible (as there is no notion of foreign key in ORM). For a better solution, see answer 2. == Answer 2 (the indirect one) == > type book = { > book_name : string; > } and chapter = { > chapter_fk_book : book; > chapter_name : string; > } with orm This is not the most natural way to define book/chapters if you would do it without persistence in mind. For instance, I would do: type book = { book_name : string; book_chapters: chapter list; } and chapter = { chapter_name = string; } with orm let get_chapters_by_book_name db book_name = match get_book db ~book_name:(`Eq book_name) with | [] -> [] | [b] -> b.chapters | _ -> failwith "too many books with the same name" and then recursive deletion works as expected. Hope this helps, Thomas
|
Lists.xenproject.org is hosted with RackSpace, monitoring our |