Reputation: 979
I am using Celery with Flask and SQLAlchemy to store some strings into the database. Everything works fine but when I created the database class:
class Locations(db.Model):
id = db.Column('id', db.Integer, primary_key=True)
data = db.Column('data', db.String(50))
def insert():
location="Madrid"
l = Locations(id=id, data=location)
db.session.add(l)
db.session.commit()
And when I access a url page is invokes the method insert(), I get this error, which I assume has something to do with id:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'builtin_function_or_method' [SQL: 'INSERT INTO locations (id, data) VALUES (%(id)s, %(data)s)'] [parameters: {'id': <built-in function id>, 'data': 'Oslo'}] (Background on this error at: http://sqlalche.me/e/f405)
Upvotes: 4
Views: 14068
Reputation: 8273
Get rid of type 'id' and 'data' and everything should work as expected.
class Locations(db.Model):
id = db.Column(db.Integer, primary_key=True)
data = db.Column(db.String(50))
def insert():
location="Madrid"
l = Locations(id=id, data=location) # You may want to remove id as well here as it will be autoincrement by default.
# So it can be l=Locations(data=location)
db.session.add(l)
db.session.commit()
Upvotes: 4