Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7539835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:29:15+00:00 2026-05-30T07:29:15+00:00

i’m just starting to learn about database design. For my first project I made

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T07:29:16+00:00Added an answer on May 30, 2026 at 7:29 am

    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 Friend and Author – 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.

    Person
    ==========
    id  -- autoincrement
    fullname  -- varchar(128) - names are tricky, keep it simple here
    nickname  -- varchar(15), nullable - optional
    
    Book
    =========
    id  -- autoincrement
    isbn  -- char(16) - check length, though
    title  -- varchar(128) - this only works with single-language libraries
    yearPublished  -- integer
    
    Book_Author
    =============
    bookId  -- fk reference to book.id
    authorId  -- fk reference to person.id
    
    Subject
    ==========
    id  -- autoincrement
    subject  -- varchar(16)
    description -- varchar(256)
    
    Book_Subject
    ===============
    bookId  -- fk reference to book.id
    subjectId  -- fk reference to subject.id
    
    Checkout
    ===============
    id  -- autoincrement
    occuredAt  -- timestamp, UTC if possible (or capture timezone)
    bookId  -- fk reference to book.id
    personId  -- fk reference to person.id
    
    Checkin
    ==============
    id  -- autoincrement
    occuredAt  -- timestamp, UTC if possible (or capture timezone)
    bookId  -- fk reference to book.id
    

    You can then tell what books you currently have on hand by which books have a Checkin record later than all Checkout records.


    EDIT:

    To ‘batch’ checkouts/ins, replace Checkout/Checkin with the following versions:

    Checkout
    ===============
    id  -- autoincrement
    occuredAt  -- timestamp, UTC if possible (or capture timezone)
    personId  -- fk reference to person.id
    
    Checkin 
    ============
    id  -- autoincrement
    occuredAt  -- timestamp, UTC if possible (or capture timezone)
    
    Checkout_Book
    ==================
    checkoutId  -- fk reference to Checkout.id
    bookId  -- fk reference to Book.id
    
    Checkin_Book
    ==================
    checkinId  -- fk reference to Checkin.id
    bookId  -- fk reference to Book.id
    

    Note that you don’t want to just add the _Book tables – you’ll need to remove the fk reference from the transaction tables as well, or you risk some nasty duplicate entries.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm making a simple page using Google Maps API 3. My first. One marker
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I want use html5's new tag to play a wav file (currently only supported
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.