LiLi LiLi - 1 year ago 161
Python Question

How do I create a table in PostgreSQL with SQLAlchemy?

I tried to create a table in the "app" database, but got this error:

app=# CREATE TABLE users;
ERROR: syntax error at or near ";"

The columns are defined in
which is why I don't create them here.

Answer Source

Do not create the tables in PostgreSQL, SQLAlchemy will create the table (and the columns) for you. All you need to do is create the database, which you have done. To create the tables with Flask-SQLAlchemy:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
db = SQLAlchemy(app)

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=False, unique=True)

with app.app_context():

To address the error (although as stated above you should not do this), it is because that is not a full CREATE TABLE expression. You need to specify zero or more columns for the table.

CREATE TABLE my_table ();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download