使用 datamapper 进行简单的租赁数据库设计
我刚刚开始学习数据库设计。对于我的第一个项目,我与 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您当前的数据模型将有一个主要缺陷 - 也就是说,所有书籍必须同时归还(好吧,不是真的,而是当第一本书归还时
Loan
'returned_at',或者在最后一个?)。Friend
和Author
之间也存在一些脱节 - 如果朋友成为作者(或作者成为朋友),会发生什么?它们最终会在您的数据库中出现两次,这是一个问题。以下是我启动图书馆数据库的方法(事实就是这样,即使您只借给朋友)。我对数据映射器一无所知,所以这些都是表设计本身。
然后,您可以通过哪些图书的
Checkin
记录晚于所有Checkout
记录来判断您当前手头上有哪些图书。编辑:
要“批量”签出/签入,请将
Checkout
/Checkin
替换为以下版本:请注意,您不想只添加
_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
andAuthor
- 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
Checkin
record later than allCheckout
records.EDIT:
To 'batch' checkouts/ins, replace
Checkout
/Checkin
with the following versions: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.