i’m just starting to learn about database design. For my first project I made a simple blog with padrino and now I want something a bit more challenging for me.
Since I am somewhat of a book nut, my friends always ask me to borrow them books. So naturally I have a lot of books floating around at any given time.
Now I’d like to have an app that lets me keep track of the books, ie: Every friend has an »Account«, I have many »Books« and my friends can rent books for any given period of time.
But I’m not entirely sure how to model the associations between the different models.
class Friend
include DataMapper::Resource
property :id, Serial
property :name, String
property :surname, String
has n, :loans
end
class Loan
include DataMapper::Resource
property :id, Serial
property :started_at, Date
property :returned_at, Date
belongs_to :friend
has n, :books
end
class Author
include DataMapper::Resource
property :id, Serial
property :name, String
property :surname, Integer
has n, :books
end
class Book
include DataMapper::Resource
property :id, Serial
property :title, String
property :year, Integer
property :returned, Boolean
belongs_to :author
belongs_to :loan
end
I’d appreciate it if you could tell me if I am on the right track with this design or maybe point me to resources that could help me.
How can I effectively manage a book being »gone« and then available again for renting?
Thanks
Your current datamodel is going to have one major flaw – that is, books must all be returned at the same time (well, not really, but is a
Loan‘returned_at’ when the first book comes back, or at the last one?).There’s also a bit of a disconnect between
FriendandAuthor– what happens if a friend becomes an author (or an author becomes a friend)? They’d end up in your database twice, which is a problem.Here’s how I’d start your library database (which is what it is, even if you only loan to friends). I don’t know anything about datamapper, so these are table designs themselves.
You can then tell what books you currently have on hand by which books have a
Checkinrecord later than allCheckoutrecords.EDIT:
To ‘batch’ checkouts/ins, replace
Checkout/Checkinwith the following versions:Note that you don’t want to just add the
_Booktables – you’ll need to remove the fk reference from the transaction tables as well, or you risk some nasty duplicate entries.