1

I'm getting a rather strange syntax error in my schema.sql file.

The error is as follows:

File "/Users/user/Documents/GitProjects/FlaskApp/flaskr/db.py", line 88, in init_db_command init_db() File "/Users/user/Documents/GitProjects/FlaskApp/flaskr/db.py", line 80, in init_db db.executescript(f.read().decode("utf-8")) sqlite3.OperationalError: near "create": syntax error

My schema looks like this:

drop table if exists post;
drop table if exists post_user;

create table post_user (
    user_id integer primary key autoincrement,
    email text unique not null,
    username text unique not null,
    password text not null
);

create table post (
    post_id integer primary key autoincrement,
    author_id integer not null,
    create timestamp not null default current_timestamp,
    title text not null,
    body text not null,
    foreign key (author_id) references post_user (user_id)
);

And my python looks like this:

def init_db():
    db = get_db()
    with current_app.open_resource("schema.sql") as f:
        db.executescript(f.read().decode("utf-8"))

And finally the directory:

root
 |--db.py
 |--schema.sql
instance
 |-- project.sqlite

I'm using flask and sqlite3. And my troubleshooting steps have been the following:

1) Remove everything except the first line (this works)

2) Removed everything except the first two lines (this doesn't)

3) Remove the ./instance/project.sqlite file

4) Use an online validator This was especially interesting because this found that each individual command works. But as soon as I string two back-to-back it said the commands were invalid.

5) Double checked the sqlite documentation on dropping tables

6) Even went so far as to ensure my .vimrc was set to utf-8

I'm sure I've missed something small here but nothing seems to be working. Any help would be appreciated!

1
  • I did? It's in the second block there. It's the thing resulting in the error. And it's what's being called by my python Commented Feb 20, 2020 at 15:07

1 Answer 1

4

You likely want create timestamp column to be something like create_timestamp to avoid clashing with sql keywords.

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

2 Comments

This was the issue. I changed the line created TIMESTAMP... to create timestamp by accident. Thank you!
Solved, in my case I was creating a table ORDER, renamed it to ORDERS and that fixed it

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.