交易目的地外键的在线信用系统表格布局
我试图弄清楚当键指向基于类型的不同表时如何正确存储外键。
有一个表将存储制作人员的大部分基本信息:
user_accounting
--------------------
user_accounting_id (PK)
user_id (FK)
amount
type (+deposit, +credit, -transfer, -purchase)
credit (bool, so I don't have to always check if amount is < or > 0)
void (in case it was canceled and repaired with some other transaction)
date
details (notes)
type s:
deposit - for outsite money being put into the system.
credit - are for money being transfered into their account from another
transfer - for putting money into someone elses account
purchase - for purchasing a site item
到目前为止一切顺利。下一部分我有点困惑。它还必须存储资金的去向或来源。
我想存储表示其来源的任何类型的外键。 因此,如果是购买,它将存储发票 ID 的 FK,如果是存款,它将存储来自商家提供商的 transaction_id,如果是转账,它将存储信用的 user_accounting_id,如果是信用,它将存储信用的 user_accounting_id。存储转账的 user_accounting_id。
我很高兴有一个列存储以下内容:
user_accounting (con't)
-----------------------------
source_or_destination_id (FK)
但我知道我不能让单个列成为基于类型链接到不同表的外键。因此,我可以将其存储为 (int),但尝试根据类型使用该 id 与不同的表进行 JOIN 会是巨大的痛苦。很久以前就尝试这样做,这是一个大错误。
因此,我可以这样做:
user_accounting (con't)
-----------------------------
invoice_id (FK)
transaction_id (FK)
credit_user_accounting_id (FK)
transfer_user_accounting_id (FK)
但这又是一个大问题,因为我创建了一个不好的独占弧。
我还可以对 type 使用 Many_to_many_through 关系,但数据透视表仍然存在将外键存储在多个表的同一位置的相同问题。
也许我可以简单地将不同类型的交易完全存储在不同的表中, 我可以:
user_accounting_deposit, user_accounting_credit, user_accounting_transfer, user_accounting_purchase.
然后外键就不再是问题了。当然,要计算出任何人的帐户余额,我现在必须对一堆表进行连接和求和。
也许有一种完全不同的更好的方法来做到这一点。我不在乎需要多少张桌子。很可能我在某个地方把事情变得过于复杂了。
谢谢
I'm trying to figure out how to properly store foreign keys when the key points to a different table based on a type.
I searched
but nothing
I found
seemed
to help
at
all.
There is a single table that will store most of the basics for the credits:
user_accounting
--------------------
user_accounting_id (PK)
user_id (FK)
amount
type (+deposit, +credit, -transfer, -purchase)
credit (bool, so I don't have to always check if amount is < or > 0)
void (in case it was canceled and repaired with some other transaction)
date
details (notes)
type s:
deposit - for outsite money being put into the system.
credit - are for money being transfered into their account from another
transfer - for putting money into someone elses account
purchase - for purchasing a site item
So far so good. This next part I'm a bit befuddled with. It will also have to store where the money is going to or coming from.
I'd like to store the foreign key for whatever type would indicate it's from.
So if it's a purchase it will store the FK for the invoice_id, if it's a deposit it'll store a transaction_id from the merchant provider, if it's a transfer it'll store the user_accounting_id of the credit, if it's a credit it'll store the user_accounting_id of the transfer.
I'd be nice to have a single column storing that:
user_accounting (con't)
-----------------------------
source_or_destination_id (FK)
But I know that I can't have a single column be a foreign key linking to different tables based on type. So I could just store it as an (int) but it would be a huge pain to try to do JOINs with that id with different tables based on the type. Tried to do that a long time ago, big mistake.
So instead, I could do this instead:
user_accounting (con't)
-----------------------------
invoice_id (FK)
transaction_id (FK)
credit_user_accounting_id (FK)
transfer_user_accounting_id (FK)
But yet again that a big problem since I create an exclusive arc which isn't good.
I could also use a many_to_many_through relation for the type but the pivot table would still have the same problem of storing foreign keys in the same spot for multiple tables.
Maybe I could simply store different types of transactions in different tables entirely,
I could have:
user_accounting_deposit, user_accounting_credit, user_accounting_transfer, user_accounting_purchase.
And then the foreign keys wouldn't be a problem. Of course to figure out anyones account balance I'd have to do joins and sums from a bunch of tables now.
Perhaps there's an entirely different better way of doing it. I don't care how many tables it takes. Chances are I'm over complicating something somewhere.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
500 多年来,簿记员一直致力于存储资金的来源和去向。这就是他们发明复式记账法的原因。也称为复式记账会计。
您需要查看复式记账的数据模型。在您之前的程序员已经这样做过数千次了。
如果您需要示例模型,请访问数据库答案并在“会计系统”下查找。您应该能够获得涵盖您的案例的模型图。
Bookkeepers have been dealing with storing where the money is coming from and where the money is going for over 500 years. That's why they invented double entry bookkeeping. Otherwise known as double entry accounting.
You need to take a look at a data model for double entry bookkeepiing. This has been done thousands of times by programmers who came before you.
If you need a sample model, visit Database Answers and look under "Accounting Systems". You should be able to get a model diagram that covers your case.