I want to make API using flask, flask-restless and flask sqlalchemy.
Database using postgres
Here's my model. The relation below is one-to-many (User have one or many
tasks. And a task is belongs to one user.
class User(Base):
__tablename__ = 'users'
id = db.Column('id', db.Integer, primary_key=True)
fullname = db.Column(db.String(40))
email = db.Column(db.String(35))
username = db.Column(db.String(20))
password = db.Column(db.String(20))
#todos = db.relationship('Todo', back_populates='user')
todos = db.relationship('Todo', backref='user', lazy='dynamic')
#todos = db.relationship("Todo", order_by=Todo.id,
back_populates="user")
def __init__(self, fullname, email, username, password):
self.fullname = fullname
self.email = email
self.username = username
self.password = password
class Todo(Base): # <--- extend dari SQLAlchemy
__tablename__ = 'todos'
id = db.Column('id', db.Integer, primary_key=True)
title = db.Column(db.String(60))
text = db.Column(db.String(100))
done = db.Column(db.Boolean, default=False, nullable=False)
pub_date = db.Column(db.DateTime)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
#user = db.relationship('User', back_populates='todos')
#user = db.relationship('User')
#user_id = db.Column(db.Integer, back_populates="todos")
#user_id = db.relationship('User', back_populates="todos")
def __init__(self, title, text, done):
self.title = title
self.text = text
self.done = done
self.pub_date = datetime.utcnow()
when I run that, it give me error:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) column
todos.user_id does not exist
I already tried several solution on sqlalchemy documentation, stack
overflow but still error and still don't get it. And I want to add more
models such as Tag, Project, Progress, Members.
My question:
1. Do I need to specify foreign key on database, or just in model is enough?
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.