2014-12-31 92 views
1

我在Flask中設置了一個應用程序,並且我剛剛爲我的數據庫模式引入了一個新表「StudyGroup」。我仍然在測試,所以我只是下降創建加載數據庫,直到一切正常。我在我的用戶模型中使用了Flask-Security,這是UserMixin部分的來源。Sqlalchemy/Flask/Postgres ProgrammingError:<unprintable ProgrammingError object>

在試圖創建和提交這些新的對象之一,是SQLAlchemy的給我一個 "ProgrammingError: <unprintable ProgrammingError object>"錯誤 和我完全難倒這意味着什麼。代碼片段及以下全回溯...

Models.py代碼:

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 

而且db_populate.py代碼: 從應用程序進口,DB,從app.models user_datastore 導入課程,用戶,Studygroup,主題事件 進口回溯 進口藏品

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() 

而且回溯:

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> 

想法?我在這裏做錯了什麼?

謝謝!

+0

請修復您的縮進。 – dirn

+0

固定縮進 –

+0

「__init__」應該是「StudyGroup」的一部分嗎? – dirn

回答

1

的錯誤可能發生的事情,你在忙線中做

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

你這樣做:

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

在數據庫中創建一個新的對象時,你在第二個例子中那樣總是用kwargs 。你錯誤的原因是這個類正在按照某種順序期待參數,你可能沒有把它們放進去。當你使用kwargs時,順序並不重要。

附註:我今天遇到了這個錯誤,當我做:

new_obj = MyObject(
    user_id=user_object 
) 

時,我應該做的

new_obj = MyObject(
    user_id=user_object.id 
) 

USER_ID期待一個整數,我給它的用戶對象。無可否認,這是sqlalchemy方面的一個可怕的錯誤消息,但這是你如何解決它的。

相關問題