1

I try to create tables from file on my schema, but i receive error message. Users table created on public schema was successfully, but other table i created on test1 schema was wrong. I don't know why, help me please. Thank for advance.

CREATE SCHEMA test1 
 --Create sequence
CREATE SEQUENCE test1.table_id_seq START 1;
--Create function to auto generate ID
CREATE OR REPLACE FUNCTION test1.next_id(OUT result bigint) AS $$
DECLARE
 our_epoch bigint := 1314220021721;
 seq_id bigint;
 now_millis bigint;
 shard_id int := 1;
BEGIN
 SELECT nextval('test1.table_id_seq') % 1024 INTO seq_id;
 SELECT FLOOR(EXTRACT(EPOCH FROM clock_timestamp()) * 1000) INTO now_millis;
 result := (now_millis - our_epoch) << 23;
 result := result | (shard_id << 10);
 result := result | (seq_id);
END;
$$ LANGUAGE PLPGSQL;
--Talbe ----users----
CREATE TABLE users
(
 id bigserial NOT NULL PRIMARY KEY,
 username varchar(30) NOT NULL UNIQUE,
 password varchar NOT NULL,
 first_name varchar(10),
 last_name varchar(10),
 profile_picture varchar,
 create_time timestamp (0) without time zone
);
--Table ----photos----
CREATE TABLE test.photos
(
 id bigint NOT NULL PRIMARY KEY DEFAULT test1.next_id(),
 caption text,
 low_resolution varchar,
 hight_resolution varchar,
 thumnail varchar,
 user_id bigint NOT NULL REFERENCES users(id),
 create_time timestamp (0) without time zone
--Table ----comments----
CREATE TABLE test1.comments
(
 id bigint NOT NULL PRIMARY KEY DEFAULT test1.next_id(),
 create_time timestamp (0) without time zone,
 text text,
 user_id bigint REFERENCES users(id),
 photo_id bigint REFERENCES test1.photos(id)
);
--Table ----likes----
CREATE TABLE test1.likes
(
 photo_id bigint REFERENCES test1.photos(id),
 user_id bigint REFERENCES users(id),
);
--Table ----follows----
CREATE TABLE test1.follows
(
 user_id bigint NOT NULL REFERENCES users(id),
 target_id bigint NOT NULL REFERENCES users(id),
);
CREATE TABLE teset1.feeds
(
 user_id bigint NOT NULL REFERENCES users(id),
 photo_id bigint NOT NULL REFERENCES test1.photos(id),
 create_time timestamp (0) without time zone,
);
asked Dec 15, 2013 at 17:57
1
  • 2
    What does "was wrong" mean? Did you get an error message? What version of PostgreSQL? Review your code for typos like this one: CREATE TABLE teset1.feeds. Commented Dec 15, 2013 at 18:03

1 Answer 1

3

Well, it would have been helpful for you to have shown us what errors you were getting and what you tried to do to fix them. But here are some obvious problems with the SQL you posted:

  1. Missing semicolon after "CREATE SCHEMA test1 "
  2. Missing closing paren and semicolon at the end of CREATE TABLE test.photos ...
  3. Dump neglects to create the "test" schema where the "photos" table wants to go.
  4. Several foreign key references of test1.photos.id, but "photos" was created in the "test" schema, not "test1".
  5. Extraneous trailing commas after the last column definitions for several tables
  6. Typo: "teset1.feeds", should be "test.feeds"

TL;DR Whoever created this dump file wasn't paying very close attention.

After fixing all the problems above, I managed to load your SQL.

answered Dec 15, 2013 at 18:54
Sign up to request clarification or add additional context in comments.

1 Comment

thank you so much Josh Kupershmidt, after fixing all the problems above, this code run well. Thanks

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.