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 6094375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:40:14+00:00 2026-05-23T12:40:14+00:00

I’m trying to figure out how to properly store foreign keys when the key

  • 0

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

  • 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-23T12:40:14+00:00Added an answer on May 23, 2026 at 12:40 pm

    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.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

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.