1

I'm setting up an app in Flask and I just introduced a new table "StudyGroup" to my database schema. I'm still testing so I'm just drop-create-loading the database until everything works. I'm using Flask-Security in my user models which is where the UserMixin part comes from.

Upon trying to create and commit one of these new objects, Sqlalchemy is giving me a "ProgrammingError: <unprintable ProgrammingError object>" error and I'm totally stumped on what it means. Code snippets and full traceback below...

Models.py code:

from app import db
from flask.ext.security import Security, SQLAlchemyUserDatastore, UserMixin, RoleMixin, login_required

class User(db.Model, UserMixin):  
    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(64), index = True)
    user_groups=db.relationship('Studygroup',
        secondary=users_x_groups,
        backref = db.backref('group_members', lazy = 'dynamic'),
        lazy='dynamic') 
    groups_founded = db.relationship("Studygroup", backref="group_founder")



class Studygroup(db.Model): #can't reduce this any further because I don't know what's blowing up
    id = db.Column(db.Integer, primary_key=True)
    created=db.Column(db.DateTime())
    founder = db.Column(db.Integer, db.ForeignKey('user.id'))
    groupname=db.Column(db.String(255))
    about = db.Column(db.Text)
    locationstr=db.Column(db.String(255))
    zipcode=db.Column(db.String(5))
    profilepiclink=db.Column(db.String(140))
    latitude=db.Column(db.Float())
    longitude=db.Column(db.Float())
    openclosed= db.Column(db.SmallInteger)
    group_topics=db.relationship('Topic',
        secondary=groups_x_topics,
        backref = db.backref('topic_groups', lazy = 'dynamic'),
        lazy='dynamic')
    group_courses=db.relationship('Course',
        secondary=groups_x_courses,
        backref = db.backref('course_groups', lazy = 'dynamic'),
        lazy='dynamic')
    group_events = db.relationship("Event", backref="studygroup")
    group_posts = db.relationship("Post", backref="studygroup")

    def __init__(self, founderid,groupname,about=None,locationstr=None,zipcode='00000',profilepiclink=None,openclosed=1,topicids=[],courseids=[]):
        self.created=datetime.datetime.utcnow()
        founder=User.query.get(founderid) #I've tried this with a User instead of an integer input, same error
        self.founder=founder
        self.groupname=groupname
        self.about=about
        self.locationstr=locationstr
        self.zipcode=zipcode
        self.profilepiclink=profilepiclink
        self.openclosed=1 #all groups default to open
        for topicid in topicids:
            self.group_topics.append(topicid)
        for courseid in courseids:
            self.group_courses.append(courseid)
        self.group_members.append(founder) 
        if zipcode=='00000':
            self.latitude=90.00
            self.longitude=0.00000
        else:
            (self.latitude, self.longitude)=eval(r.get("zipcode:"+str(zipcode))) #TESTING

And the db_populate.py code: from app import app, db, user_datastore from app.models import Course, User, Studygroup, Topic, Event import traceback import collections

def dropit_popit():
    db.session.commit() #prevents an error
    db.drop_all()
    with app.app_context():
        db.create_all()
    u2=collections.defaultdict()
    u2[0] = MakeUserInDb('Alex','<email redacted>','password','94110')
    for x in range(1,20):
        if x%10==0: print x
        if x%10: db.session.commit()
        usethiszip=random.choice(possiblezips)
        u2[x]=MakeUserInDb(username="TEST", email='User'+str(x)+'@User.com', password='password', zipcode=usethiszip)
    uuuu=User.query.get(1) #Earlier, tried using User instead of userid with lookup to fix
    print uuuu  #returns the first user I made

    #note: All of the above works for creating users etc. I recently introduced Studygroup and that's what I'm testing.

    try:
        dat_groop=Studygroup(1,"TESTGROUP",about="Dis be group",locationstr="Frisco",zipcode='94110',profilepiclink=None,openclosed=1,topicids=[],courseids=[])
        db.session.add(dat_groop)
        db.session.commit() #THIS IS WHERE THE ERROR HAPPENS
    except:
        print traceback.print_exc()

And the traceback:

Traceback (most recent call last):

      File "db_populate.py", line 139, in dropit_popit
    db.session.commit()
  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/scoping.py", line 114, in do
    return getattr(self.registry(), name)(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/session.py", line 703, in commit
    self.transaction.commit()
  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/session.py", line 361, in commit
    self._prepare_impl()
  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/session.py", line 340, in _prepare_impl
    self.session.flush()
  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/session.py", line 1718, in flush
    self._flush(objects)
  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/session.py", line 1789, in _flush
    flush_context.execute()
  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 331, in execute
    rec.execute(self)
  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 475, in execute
    uow
  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/persistence.py", line 64, in save_obj
    table, insert)
  File "/Library/Python/2.7/site-packages/sqlalchemy/orm/persistence.py", line 558, in _emit_insert_statements
    execute(statement, params)
  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 1449, in execute
    params)
  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 1584, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 1698, in _execute_context
    context)
  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/base.py", line 1691, in _execute_context
    context)
  File "/Library/Python/2.7/site-packages/sqlalchemy/engine/default.py", line 331, in do_execute
    cursor.execute(statement, parameters)
ProgrammingError: <unprintable ProgrammingError object>

Thoughts? What am I doing wrong here?

Thanks!

6
  • Please fix your indentation.
    – dirn
    Dec 31, 2014 at 4:34
  • Is __init__ supposed to be part of StudyGroup?
    – dirn
    Dec 31, 2014 at 16:13
  • Agh - missed that layer. __init__ is part of Studygroup. (I'm new at stackoverflow - is there a better way than indenting code four spaces manually after copy/pasting it?)
    – Alex M
    Dec 31, 2014 at 22:21
  • You can highlight the code and click the {} button.
    – dirn
    Dec 31, 2014 at 23:21
  • 1
    Can you please reduce the code to a Minimal, Complete, and Verifiable Example?
    – dirn
    Dec 31, 2014 at 23:24
3

The error is probably happening where you do

MakeUserInDb('Alex','<email redacted>','password','94110')

on another line you do:

MakeUserInDb(username="TEST", email='User'+str(x)+'@User.com', password='password', zipcode=usethiszip)

Always use kwargs when creating a new object in the database as you did in the second example. The reason for your error is the class is expecting arguments in a certain order that you are probably not putting them in. When you use kwargs the order doesn't matter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.