31
loading...
This website collects cookies to deliver better user experience
Account
. A customer can be a person or a company. This also means a bank would support different types of accounts: CompanyAccount
and a SavingsAccount
– to keep this analogy simple.The 2 types of accounts will have shared attributes – id, address, name – and unique properties – company name, owners etc. Account
that would contain all the shared attributes. The child entities CompanyAccount
and SavingsAccount
would inherit the parent entity's attributes and define their own attributes.type
or discriminator
property is introduced in the parent entity which is a string value.class Account {
// define the attributes here
type: string // "CompanyAccount" or "SavingsAccount"
}
class CompanyAccount extends Account {
// define the attributes here
signatories: Json
}
class SavingsAccount extends Account {
// define the attributes here
signatory: string
}
Account
– is created. All the attributes of the child entities would be added to the Account
table and marked as nullable or NULL
.CREATE TABLE "Account" (
"id" SERIAL NOT NULL,
"balance" DOUBLE PRECISION NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"type" TEXT NOT NULL,
-- SavingsAccount specific field
"signatory" TEXT,
-- CorporateAccount specific field
"signatories" JSONB,
CONSTRAINT "Account_pkey" PRIMARY KEY ("id")
);
CREATE INDEX "account_type_index" on "content" ("type");
Account
.NULL
fields.NOT NULL
constraint.type
column in the parent entity to determine the type of child entityUNION
on child tablesCREATE TABLE "SavingsAccount" (
"id" SERIAL NOT NULL,
"balance" DOUBLE PRECISION NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"signatory" TEXT NOT NULL,
CONSTRAINT "SavingsAccount_pkey" PRIMARY KEY ("id")
);
CREATE TABLE "CorporateAccount" (
"id" SERIAL NOT NULL,
"balance" DOUBLE PRECISION NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"signatories" JSONB NOT NULL,
CONSTRAINT "CorporateAccount_pkey" PRIMARY KEY ("id")
);
INHERITS
keyword that takes care of inheritance. The only difference is that the parent entity is also created in the databaseCREATE TABLE ACCOUNT (
-------- Base table properties here
);
CREATE TABLE CORPORATE_ACCOUNT (
-------- Specific table properties
) INHERITS(ACCOUNT);
CREATE TABLE SAVINGS_ACCOUNT (
-------- Specific table properties
) INHERITS(ACCOUNT);
-- CreateTable
CREATE TABLE "Account" (
"id" SERIAL NOT NULL,
"balance" DOUBLE PRECISION NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"savingsAccountId" INTEGER NOT NULL,
"corporateAccountId" INTEGER NOT NULL,
CONSTRAINT "Account_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "SavingsAccount" (
"id" SERIAL NOT NULL,
"signatory" TEXT NOT NULL,
"accountId" INTEGER,
CONSTRAINT "SavingsAccount_pkey" PRIMARY KEY ("id")
);
-- CreateTable
CREATE TABLE "CorporateAccount" (
"id" SERIAL NOT NULL,
"signatories" JSONB NOT NULL,
"accountId" INTEGER,
CONSTRAINT "CorporateAccount_pkey" PRIMARY KEY ("id")
);
-- CreateIndex
CREATE UNIQUE INDEX "Account_savingsAccountId_key" ON "Account"("savingsAccountId");
-- CreateIndex
CREATE UNIQUE INDEX "Account_corporateAccountId_key" ON "Account"("corporateAccountId");
type
column in the parent entity to determine the type of child entityUNION
stype
of a child entity without querying them. However, if you're using an ORM is a non-issue