从架构PostgreSQL上的sql文件创建表

从架构PostgreSQL上的sql文件创建表,postgresql,Postgresql,我试图从架构上的文件创建表,但收到错误消息。已成功在公共架构上创建Users表,但我在test1架构上创建的另一个表是错误的。我不知道为什么,请帮帮我。 谢谢你的预付款 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_

我试图从架构上的文件创建表,但收到错误消息。已成功在公共架构上创建Users表,但我在test1架构上创建的另一个表是错误的。我不知道为什么,请帮帮我。 谢谢你的预付款

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,
);
创建模式test1
--创建序列
创建序列test1.table_id_seq START 1;
--创建自动生成ID的函数
创建或替换函数test1.next_id(OUT result bigint)作为$$
声明
我们的_epoch bigint:=1314220021721;
seq_id bigint;
现在_millisbigint;
shard_id int:=1;
开始
选择nextval('test1.table_id_seq')%1024进入seq_id;
选择地板(从时钟时间戳()提取历元*1000)到现在的毫秒;

结果:=(现在_millis-我们的_epoch)好吧,如果你能告诉我们你得到了什么错误,以及你试图做些什么来修复这些错误,那会很有帮助。但您发布的SQL存在一些明显的问题:

  • “创建架构test1”后缺少分号
  • CREATE TABLE test.photos结尾缺少结束符和分号
  • Dump忽略了在“photos”表想要去的地方创建“test”模式
  • test1.photos.id的几个外键引用,但“photos”是在“test”模式中创建的,而不是在“test1”中创建的
  • 多个表的最后一列定义后的多余尾随逗号
  • 输入错误:“teset1.feeds”,应该是“test.feeds”
  • TL;DR创建此转储文件的人没有密切注意


    在解决了上述所有问题后,我成功加载了您的SQL。

    什么是“错误”呢?你收到错误消息了吗?什么版本的PostgreSQL?检查您的代码是否有以下错误:
    创建表teset1.feeds
    。非常感谢Josh Kupershmidt,在解决了上述所有问题后,此代码运行良好。谢谢