I run my code and get the error in PostgreSQL. I have created multiple tables and the first seems to work while the others don't.
create table people (
id serial,
family_names text,
given_names text,
displayed_names text,
email_address text,
primary key (id)
);
create table users (
userid integer,
website text,
date_registered date,
gender GenderValue,
birthday date,
password text,
portrait integer,
primary key (userid),
foreign key (userid) references people(id),
foreign key (portrait) references photos(photoid)
);
create table photos (
photoid serial,
title TitleValue,
date_uploaded date,
date_taken date,
description text,
technical_details text,
safety_level safetyLevel,
visibility visibilityLevel,
owns integer,
primary key (photoid),
foreign key (owns) references users(userid)
);
Related
I tried using a foreign key, but it still shows them as being separate and not connected is there something I'm missing here, my goal was to originally link the persons name to the account_balance.
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,
CONSTRAINT bank_users
FOREIGN KEY (id)
REFERENCES accounts(id)
ON DELETE CASCADE
);
INSERT INTO accounts (
first_name,
last_name,
username,
password,
account_Type,
bday)
VALUES ('bob', 'john', 'bob#gmail.com', crypt('bob1',gen_salt('bf')),'Manager', '01/01/1985'),
('Tom', 'lin', 'tom#gmail.com', 'tom1', 'Manager', '5/23/1990');
INSERT INTO bank_account(
account_number,
account_balance)
VALUES ('1234', '50000'),('4332', '100000');
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 this table
create table preñadas(
hierro varchar(15) NOT NULL,
hierro_toro varchar (30) NOT NULL,
fecha_esperada_parto timestamp,
observaciones varchar(200),
primary key (hierro),
foreign key (hierro) references animales,
foreign key (hierro_toro) references animales (hierro)
);
i would like to eliminate a record automatically from it when now() is one month past fecha_esperada_parto
Any ideas how to do it?
Here is my schema for database tables:
CREATE TABLE users (
user_id INTEGER NOT NULL,
device_id INTEGER,
user_points INTEGER,
cookie VARCHAR,
PRIMARY KEY (user_id)
);
CREATE TABLE admins (
admin_id INTEGER NOT NULL,
username VARCHAR,
password VARCHAR
);
CREATE TABLE admin_adventure (
adventure_id INTEGER NOT NULL,
admin_id INTEGER,
PRIMARY KEY (adventure_id)
);
CREATE TABLE adventures (
adventure_id INTEGER NOT NULL,
prize_id INTEGER,
novella_id INTEGER,
PRIMARY KEY (adventure_id)
);
Here I'm trying to define FOREIGN KEYS:
ALTER TABLE admin_adventure ADD FOREIGN KEY ( admin_id ) REFERENCES admins ( admin_id );
ALTER TABLE admin_adventure ADD FOREIGN KEY ( adventure_id ) REFERENCES adventures ( adventure_id );
And here is the error I get when trying to migrate with Flyway:
ERROR: there is no unique constraint matching given keys for
referenced table "admins"
Can someone explain what I'm doing wrong and why I get this error?
The error message is complaining that you are trying to link a foreign key to a column in another table which is not unique (e.g. a primary key). Try making the admin_id column in the admins table a primary key:
CREATE TABLE admins (
admin_id INTEGER NOT NULL,
username VARCHAR,
password VARCHAR,
PRIMARY KEY (admin_id)
);
Like the documentation says:
A foreign key must reference columns that either are a primary key or form a unique constraint.
So you must either
ALTER TABLE admins ADD PRIMARY KEY (admin_id);
or
ALTER TABLE admins ADD UNIQUE (admin_id);
i´ve got a problem with foreign keys, it´s an strange problem.
First table:
CREATE TABLE adjunto
(
id serial NOT NULL,
codigo text,
descripcion text,
usuario integer,
file integer,
nombre text,
propiedades hstore,
CONSTRAINT adjunto_pkey PRIMARY KEY (id ),
CONSTRAINT adjunto_file_fkey FOREIGN KEY (file)
REFERENCES file (file_id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
) WITH (
OIDS=FALSE
);
Second table:
CREATE TABLE adjunto_coleccion_privada
(
id serial NOT NULL,
adjunto integer,
coleccion integer,
CONSTRAINT adjunto_coleccion_privada_pkey PRIMARY KEY (id ),
CONSTRAINT adjunto_coleccion_privada_adjunto_fkey FOREIGN KEY (adjunto)
REFERENCES adjunto (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE,
CONSTRAINT adjunto_coleccion_privada_coleccion_fkey FOREIGN KEY (coleccion)
REFERENCES coleccion (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE CASCADE
)
WITH (
OIDS=FALSE
);
Command:
INSERT INTO adjunto_coleccion_privada (adjunto, coleccion)
VALUES (600, 2) RETURNING id
Values 600 and 2 exist in both tables, adjunto and colecion.
Detailed error:
Mensaje: ERROR: insert or update on table "adjunto_coleccion_privada"
violates foreign key
constraint "adjunto_coleccion_privada_adjunto_fkey"
Detail: Key (adjunto)=(600) is not present in table "adjunto".
I tested your code (I dropped the adjunto_coleccion_privada_coleccion_fkey constraint since referred table does not exist in your pasted code).
I see no problem at all.
Are you really sure that there is a record with id = 600 in the adjunto table?