relation tables inheritance modeling - postgresql

I want to understand inheritance in postgresql, simple whitch columns in whitch tables.
CREATE TABLE users (
id serial PRIMARY KEY,
username VARCHAR UNIQUE NOT NULL,
email VARCHAR NOT NULL,
password_salt VARCHAR,
password_hash VARCHAR,
avatar serial
)
CREATE TABLE groups (
id serial PRIMARY KEY NOT NULL,
name VARCHAR,
email VARCHAR,
avatar serial,
)
CREATE TABLE accounts (
id serial PRIMARY KEY NOT NULL,
name VARCHAR,
avatar serial,
rating json NOT NULL,
);
CREATE TABLE users_to_accounts (
id serial PRIMARY KEY NOT NULL,
start_time DATETIME NOT NULL,
end_time DATETIME,
)
CREATE TABLE account_subscryptions (
user_id serial NOT NULL,
account_id serial NOT NULL,
) INHERITS users_to_accounts
CREATE TABLE account_memberships (
user_id serial NOT NULL,
account_id serial NOT NULL,
) INHERITS users_to_accounts
CREATE TABLE users_to_groups (
id serial PRIMARY KEY NOT NULL,
start_time DATETIME NOT NULL,
end_time DATETIME,
)
CREATE TABLE group_subscryptions (
user_id serial NOT NULL,
group_id serial NOT NULL,
) INHERITS users_to_groups
CREATE TABLE group_memberships (
user_id serial NOT NULL,
group_id serial NOT NULL,
) INHERITS users_to_groups
Now.
1. Is it good design to have foreign keys in child tables an all common data in "abstract" table?
2. Is there any traps in future changes of database with inherited relantion tables?
3. I am all wrong and there is a better way for this schema?
4. I want to create good database schema and generate graphql api in postgraphile, looking in google for half day, did not gave me any one good or best solution, so every link will by great.
It may be usefull for others, I think. Thanks

Related

How to upsert into a table without setting an index?

Suppose the following setup:
create table chat_message (
id serial primary key,
message_id bigint unique not null, -- id from third party service
message_type varchar(255) unique not null
);
I can upsert into this table like this:
-- insert first record
insert into chat_message
values(1, 1234, 'start_message');
-- upsert
insert into chat_message (message_id, message_type)
values(1260, 'start_message')
on conflict (message_type)
do update set message_id = 1260;
This works fine, but I need to structure the table like this:
create table chat_message (
id serial primary key,
message_id bigint unique not null,
message_type varchar(255) not null -- this isn't necessarily unique
);
How can I accomplish this? Sure, message_id will always be unique, but there could be many messages with message_type = 'start_message'

How do you create the table structure in PostgreSQL to make a many-to-many relationship

my example so far I would like to know how to create a many to many relationship with these.
CREATE TABLE accounts (
id SERIAL,
first_name VARCHAR(50),
last_name VARCHAR (50),
username VARCHAR (50),
password VARCHAR (500),
account_Type VARCHAR (10),
bday VARCHAR (50),
PRIMARY KEY(id)
);
CREATE TABLE bank_account(
id SERIAL,
account_number INTEGER PRIMARY KEY,
account_balance INTEGER
);
you can create a table like this one:
CREATE TABLE bank_account_accounts(
id serial primary key,
accounts_id integer,
bank_account_id integer
);

TypeORM: many to many: Get all teams from userID

I have two table: User and Team.
They are in a many-to-many relation.
I would like to get all teams of a specific user.
Seams really simple but I can't find the answer anywhere..
Do you know how can I do ?
On the many-to-many relation must be 3rd table. This table contained user_id and team_id connections.
For example:
CREATE TABLE user (
id serial4 NOT NULL,
first_name varchar(200) NOT NULL,
last_name varchar(200) NULL,
CONSTRAINT user_pk PRIMARY KEY (id)
);
CREATE TABLE team (
id serial4 NOT NULL,
team_name varchar NOT NULL,
team_about text NULL,
CONSTRAINT team_pk PRIMARY KEY (id)
);
CREATE TABLE user_team (
id serial4 NOT NULL,
user_id int4 NOT NULL,
team_id int4 NOT NULL,
CONSTRAINT user_team_pk PRIMARY KEY (id)
);
-- filter and select team by user_id
select t.* from examples.team t
inner join examples.user_team usrt on usrt.team_id = t.id
where usrt.user_id = 2;

Homegraph concept database schema permissions

I'm trying to make schema for my PostgreSQL database basing on the Homegraph concept, however I'm not really sure about correctness of current schema and whether user should have per-structure access or per-device.
I can't find any real-world example of Homegraph concept implementation, and Google seem to not provide such information.
CREATE EXTENSION hstore;
CREATE TABLE users (
id CHAR(32) NOT NULL,
username TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
password_hash TEXT NOT NULL,
PRIMARY KEY (
id
)
);
CREATE TABLE user_structures (
structure_id CHAR(32) REFERENCES structures (id) ON DELETE CASCADE,
user_id CHAR(32) REFERENCES users (id) ON DELETE CASCADE,
manager BOOL NOT NULL,
PRIMARY KEY (
structure_id,
user_id,
)
);
CREATE TABLE structures (
id CHAR(32) UNIQUE NOT NULL,
label TEXT NOT NULL,
PRIMARY KEY (
id
)
);
CREATE TABLE rooms (
id CHAR(32) UNIQUE NOT NULL,
structure_id CHAR(32) REFERENCES structures (id) ON DELETE CASCADE,
label TEXT NOT NULL,
PRIMARY KEY (
room_id
)
);
CREATE TABLE devices (
id CHAR(32) UNIQUE NOT NULL,
room_id CHAR(32) REFERENCES room (id) ON DELETE CASCADE,
password_hash TEXT NOT NULL,
type TEXT NOT NULL,
traits TEXT[] NOT NULL,
name TEXT NOT NULL,
will_push_state BOOL NOT NULL,
model TEXT NOT NULL,
hw_version TEXT NOT NULL,
sw_version TEXT NOT NULL,
attributes hstore NOT NULL,
PRIMARY KEY (
id,
structure_id
)
);
With regard to the devices table, there is a canonical protobuf that defines each of the fields and their types.
Beyond that, the implementation of the HomeGraph schema is an internal definition within Google's smart home platform and its structure shouldn't matter from a developer perspective.

Many-to-one relationship using two unique keys

I have two tables which look like thiS:
uploads
________
id (primary key)
user_id
file_checksum
upload_information
---------
upload_info_id (primary key)
file_checksum
metadata1
metdata2
The "many to one" relationship I am trying to enforce is this:
Many uploads can have the same file checksum
However, the file checksum can only ever point to one upload_information record, thus making the unique constraint between file_checksum and upload_info_id mandatory in the upload_information table.
I am wondering how to alter these tables in Postgres in order to achieve this relationship.
CREATE TABLE uploads (
id SERIAL NOT NULL PRIMARY KEY,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
user_id VARCHAR NOT NULL,
file_checksum VARCHAR NOT NULL,
);
CREATE TABLE upload_information (
upload_info_id SERIAL NOT NULL PRIMARY KEY,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
file_checksum VARCHAR NOT NULL,
file_name VARCHAR NOT NULL,
source_file_url VARCHAR NOT NULL,
);
Add a unique index on file_checksum.
create unique index unique_checksum on upload_information(file_checksum)