0

I'm building a database for a site and I keep getting an incorrect syntax error for this portion of it:

    CREATE TABLE posts (
        pid SERIAL PRIMARY KEY,
        title VARCHAR(255),
        body VARCHAR,
        user_id INT REFERENCES users(uid),
        author VARCHAR REFERENCES users(username),
        date_created TIMESTAMP
        like_user_id INT[] DEFAULT ARRAY[]::INT,
        likes INT DEFAULT
    );

When I put a comma after TIMESTAMP, it then switches the error message to after the INT [] on the like_user_id line.

1
  • Tag your question with the database you are using. The syntax is definitely not standard SQL. Also, your foreign key references do not make sense. Why would you have separate columns for user_id and author with two references to the same table using different columsn? Commented Dec 28, 2020 at 21:40

2 Answers 2

1

Assuming you are using Postgres (which the syntax is closest to), you can use something like this:

CREATE TABLE posts (
    pid SERIAL PRIMARY KEY,
    title VARCHAR(255),
    body VARCHAR(255),
    author_uid INT REFERENCES users(uid),
    date_created TIMESTAMP,
    like_user_id INT[] DEFAULT '{}'::INT[],
    likes INT DEFAULT 0
);

Notes:

  • There is no need for a separate foreign key reference for the user and the name. Use a join to get the name.
  • You need a comma after the timestamp.
  • You need an appropriate expression for an empty array for like_user_id.
  • You need a default value for likes.

I also recommend putting in lengths for the varchar(). I mean, you can skip all the lengths if you like (Postgres allows that), but I don't see a reason to have lengths in some columns and no lengths in others.

Sign up to request clarification or add additional context in comments.

Comments

0

Cast ARRAY[]::INT => ARRAY[]::INT[]:

 CREATE TABLE posts (
        pid SERIAL PRIMARY KEY,
        title VARCHAR(255),
        body VARCHAR,
        user_id INT REFERENCES users(uid),
        author VARCHAR REFERENCES users(username),
        date_created TIMESTAMP,
        like_user_id INT[] DEFAULT array[]::int[],
        likes INT DEFAULT 0
    );

db<>fiddle demo

Comments

Your Answer

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.