使用 datamapper 进行简单的租赁数据库设计

发布于 2025-01-08 02:08:48 字数 979 浏览 0 评论 0原文

我刚刚开始学习数据库设计。对于我的第一个项目,我与 padrino 制作了一个简单的博客,现在我想要一些对我来说更具挑战性的东西。 由于我是一个书迷,所以我的朋友总是找我借书。所以很自然地,我随时都会有很多书在身边。

现在我想要一个可以让我跟踪书籍的应用程序,即:每个朋友都有一个“帐户”,我有很多“书籍”,并且我的朋友可以在任何给定时间段内租借书籍。 但我并不完全确定如何对不同模型之间的关联进行建模。

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'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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心是晴朗的。 2025-01-15 02:08:48

您当前的数据模型将有一个主要缺陷 - 也就是说,所有书籍必须同时归还(好吧,不是真的,而是当第一本书归还时Loan'returned_at',或者在最后一个?)。

FriendAuthor 之间也存在一些脱节 - 如果朋友成为作者(或作者成为朋友),会发生什么?它们最终会在您的数据库中出现两次,这是一个问题。

以下是我启动图书馆数据库的方法(事实就是这样,即使您只借给朋友)。我对数据映射器一无所知,所以这些都是表设计本身。

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

然后,您可以通过哪些图书的 Checkin 记录晚于所有 Checkout 记录来判断您当前手头上有哪些图书。


编辑:

要“批量”签出/签入,请将 Checkout/Checkin 替换为以下版本:

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

请注意,您不想只添加 _Book 表 - 您还需要从事务表中删除 fk 引用,否则您将面临一些令人讨厌的重复条目的风险。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文