Open MarkusWendorf opened 1 year ago
Thanks for the suggestion, this looks useful. We'll discuss it internally.
+1 to INHERITS
+1 to Inherits
Would like this feature as well
This would be great. Like @SebastianGarces mentioned: For Postgres specifically, it would be great if we had the native table inheritance: https://www.postgresql.org/docs/current/tutorial-inheritance.html
CREATE TABLE cities (
name text,
population real,
elevation int -- (in ft)
);
CREATE TABLE capitals (
state char(2) UNIQUE NOT NULL
) INHERITS (cities);
ALTER TABLE cities
ADD test_id varchar(255); -- Both table would contains test col
DROP TABLE cities; -- Cannot drop because capitals depends on it
ALTER TABLE cities
ADD CONSTRAINT fk_test FOREIGN KEY (test_id) REFERENCES sometable (id);
It could be implement via pgTable().inherits()
, example:
// Parent table (cities)
export const cities = pgTable('cities', {
name: text('name').notNull(),
population: real('population'),
elevation: integer('elevation'),
})
// Child table (capitals) inheriting from cities
export const capitals = pgTable('capitals', {
state: char('state', { length: 2 }).notNull().unique(),
}).inherits(cities)
+1 for inherits. Even simple single table inheritance would be super useful
+1 to this one
+1
Describe what you want
Hello,
I would really like to see support for single table inheritance in drizzle.
Single table inheritance is a way to save inheritance structures to a single database table. Imagine a
accommodation
class in OOP, which can be either ahotel
or amotel
. Both sub-classes inherit a set of properties from the parent (onlyid
in this case) but each class also has additional fields (hotelName
ormotelName
) that are unique to their specific class.You could save it in the database like this:
Prior work in other ORMs: TypeORM Node.js CycleORM PHP Hibernate Java
Possible API:
The output type should be a discriminated union for:
type Table = Hotel | Motel
based on the value oftype
. Not sure how well this API works out in practice (type inference, relations?).Thanks