I have a products table
CREATE TABLE products (
id BIGSERIAL NOT NULL PRIMARY KEY,
title VARCHAR(100) NOT NULL,
description TEXT NULL,
price BIGINT NOT NULL
);
How to make so that when adding/editing PostgreSQL's data is automatically updated in Elasticsearch?
Related
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.
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)
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
This is myposgres.sql file in my project directory:
CREATE TABLE client (
cli_id serial PRIMARY KEY,
cli_name text NOT NULL,
cli_birthyear smallint NOT NULL
);
CREATE TABLE product (
pro_id serial PRIMARY KEY,
pro_ean text UNIQUE NOT NULL,
pro_name text NOT NULL,
pro_description text NOT NULL,
pro_weight_g smallint NOT NULL
);
CREATE TABLE command (
com_id serial PRIMARY KEY,
com_number text UNIQUE NOT NULL,
cli_id integer NOT NULL REFERENCES client
);
CREATE TABLE command_line (
lin_id serial PRIMARY KEY,
com_id integer NOT NULL REFERENCES command,
pro_id integer NOT NULL REFERENCES product,
lin_quantity smallint NOT NULL
);
Now, how do I use this file to connect the postges database to graphql? And how query postgres database using graphql?
In postgresql I have a table which I need to add a new column. the original table ddl is belowing:
CREATE TABLE survey.survey_response (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
CONSTRAINT survey_response_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
) ;
Then I alter the table to add a new column:
alter table survey.survey_response add column system_tags varchar(30) ;
But after that I found my instert statement cannot make change to this new column, for all the original columns it works fine:
INSERT INTO survey.survey_response
(id, survey_id, user_id, tags, system_tags)
VALUES(uuid_generate_v4(), uuid_generate_v4(),'1123','dsfsd', 'dsfsd');
select * from survey.survey_response where user_id = '1123';
The "tags" columns contains inserted value, however, system_tags keeps null.
I tested the above scenario in my local postgreSQL 9.6, any ideas about this strange behavior? Thanks a lot
-----------------update----------
I found this survey.survey_response table has been partitioning based on month, So my inserted record will also be displayed in survey.survey_response_y2017m12. but the new system_tags column is also NULL
CREATE TABLE survey.survey_response_y2017m12 (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
system_tags varchar(30) NULL,
CONSTRAINT survey_response_y2017m12_response_date_check CHECK (((response_date >= '2017-12-01'::date) AND (response_date < '2018-01-01'::date)))
)
INHERITS (survey.survey_response)
WITH (
OIDS=FALSE
) ;
If I run the same scenario in a non-partition table then the insert works fine.
So do I need any special settings for alter table for partition table?
Old thread but you need to drop and create again the RULE to fix the issue.