2014-04-22 3 views
4

두 테이블을 연결하여 외래 키가있는 양식을 만들려고합니다.오류 메시지 : InterfaceError : <인쇄 할 수없는 InterfaceError 개체>

sqlalchemy.exc.InterfaceError

InterfaceError : 인쇄 할 수없는 InterfaceError 객체

DATBASE 링크는 수행과 같은 :

class Client(db.Model): 
__tablename__ = 'Client' 
.. 
stand_id = db.Column(db.String(10), index = True, unique = True) 
stands = db.relationship('Stand', backref= 'Stand', lazy='select') 


def __init__(self,client_name,contact_number,contact_name,contact_email,stand_id): 
    self.client_name = client_name 
    self.contact_number = contact_number 
    self.contact_email = contact_email 
    self.contact_name = contact_name 
    self.stand_id = stand_id 

class Stand(db.Model): 
__tablename__ = 'Stand' 
.. 
stand_number = db.Column(db.String(10), db.ForeignKey('Client.stand_id')) 

def __repr__(self): 
    return '<Stand %r>' % (self.stand_id) 


def __init__(self, stand_name,items, quantity,install_date, 
derig_date,comments,last_update, stand_number): 
self.stand_name = stand_name 
self.items = items 
self.quantity = quantity 
self.install_date = install_date 
self.derig_date = derig_date 
self.comments = comments 
self.last_update = last_update 
self.stand_number = stand_number 

양식

class StandForm(Form): 
stand_name = TextField('stand_name', validators = [Required()]) 
items = TextAreaField('items', validators = [Required()]) 
quantity = TextAreaField('quantity', validators = [Required()]) 
install_date = TextField('install_date',validators = [Required()]) 
derig_date = TextField('derig_date', validators = [Required()]) 
comments = TextField('comments', validators = [Required()]) 
last_update = TextField('last_update', validators = [Required()]) 
stand_number = QuerySelectField(query_factory=lambda: Client.query.all()) 

로 및보기
@app.route('/newstand', methods = ['GET','POST']) 
def newstand(): 
form = StandForm() 
if form.validate(): 
    stand = Stand(
    request.form['stand_name'], request.form['items'], request.form['quantity'], 
    request.form['install_date'],request.form['derig_date'], request.form['comments'], 
    request.form['last_update'], request.form['stand_number']) 
    form.populate_obj(stand) 
    db.session.add(stand) 
    db.session.commit() 
    return render_template('liststands.html', 
     stand = stand, 
      form=form) 
else: 
    flash("Your form contained errors") 
return render_template('newstand.html', form = form 

나는이 함수를 아주 올바르게 작성했다고 생각하지 않는다. 어떤 의견이나 도움이 될까?

+0

하는 SQL 엔진을 사용합니까를? – silpol

답변

2

나는이 같은 오류가 발생하여 비슷한 문제가있는 것처럼 보입니다. QuerySelectField은 ID가 아닌 전체 개체를 반환합니다. 위의 예를 들어, 위의 예를 또한

@app.route('/newstand', methods = ['GET','POST']) 
def newstand(): 
    form = StandForm() 
    if form.validate(): 
    stand = Stand(
     request.form['stand_name'], request.form['items'], request.form['quantity'], 
     request.form['install_date'],request.form['derig_date'], request.form['comments'], 
     request.form['last_update'], request.form['stand'].stand_id) 
    ... 

:

class StandForm(Form): 
    ... 
    stand = QuerySelectField(query_factory=lambda: Client.query.all()) 

을 그리고 당신의보기에서 개체를 구성하는 ID를 사용

가 포함 된 내용을 반영하기 위해 양식 필드의 이름을 바꿉니다 양식 내용이있는 Stand 개체를 명시 적으로 작성하고 form.populate_obj을 다시 호출하여 중복되는 경우 (개체의 동일한 필드를 모두 다시 채 웁니다) 이러한 개체 중 하나를 제거 할 가능성이 있습니다.

form.populate_obj가 자동으로 외래 키 (나는 그것을 시도하지 않은 비록 내가 그것을하지 가정 거라고하는), 다음에보기 코드를 단순화 할 수 존중 경우

@app.route('/newstand', methods = ['GET','POST']) 
def newstand(): 
    form = StandForm() 
    if form.validate(): 
    stand = Stand() 
    form.populate_obj(stand) 
    ... 
+0

어떤 SQL 엔진을 사용합니까? – silpol

+0

@ Jeremy Nikolai : 동일한 상황이 발생하며 'QuerySelectField'도 사용합니다. [this] (http://stackoverflow.com/questions/43908969/flask-admin-sqlalchemy-exc)를 참조하십시오. -interfaceerrorrerror-binding-parameter-8), 감사합니다. – Samoth

관련 문제