flask admin : sqlalchemy.exc.InterfaceError(Error binding parameter 8)
Asked Answered
P

1

-1

I was trying to save a project's reviewer using below, and the select field shows correct:

# Query the user with Role.id == 4 as reviewer
def reviewer_choices():
    return User.query.join(User.roles).filter(Role.id == 4)

# Build a select field
class ProjectView(sqla.ModelView):
 form_extra_fields = {
    'reviewer': sqla.fields.QuerySelectField(
    label='Reviewer',
    query_factory=reviewer_choices,
 )}

However, when I was trying to save it, the error occurred:

InterfaceError: (sqlite3.InterfaceError) Error binding parameter 8 - probably unsupported type. [SQL: u'INSERT INTO project(...reviewer...)VALUES(...<__main__.User object at 0x00000000048E89E8>...).

And I noticed that the reviewer is an object, something like: <__main__.User object at 0x00000000048E89E8>. So what is the correct data type of reviewer so I can save it into database? I currently used:

In the project class

class Project(db.Model):
   # ...
   reviewer = db.Column(db.Unicode(128)) 
   # ...

In the project table

CREATE TABLE `project` (
   # ...
   `reviewer1`  TEXT,
   # ...

And I also tried to define __repr__ and __str__ but both not worked:

class User(db.Model, UserMixin):
   id = db.Column(db.Integer, primary_key=True)
   first_name = db.Column(db.String(255))
   # ...
   # ...
   def __repr__(self):
     return self.first_name

class User(db.Model, UserMixin):
   id = db.Column(db.Integer, primary_key=True)
   first_name = db.Column(db.String(255))
   # ...
   # ...
   def __str__(self):
     return self.first_name
Parenthesis answered 11/5, 2017 at 7:21 Comment(0)
A
0

It's obviously. The reviewer is a string defined in Project. You can define a __repr__ function in User model to return a string(name of user or something else). Check this question.

Ablaze answered 11/5, 2017 at 7:40 Comment(3)
Thanks, but it still not work and return InterfaceError: <unprintable InterfaceError object> this time.Parenthesis
Try define __str__ function in User model.Ablaze
Yes, I was using __str__ and then switch to __repr__ but both did not work.Parenthesis

© 2022 - 2024 — McMap. All rights reserved.