danywigglebutt
danywigglebutt

Reputation: 269

Sqlalchemy "Incorrect integer value: 'VARIABLE' for column 'id' at row 1"

Whenever I try to use the standard "add" functionality on Flask's SQLAlchemy wrapper I get the following error:

OperationalError: (_mysql_exceptions.OperationalError) 
(1366, "Incorrect integer value: 'username' for column 'id' at row 1") 

[SQL: u'INSERT INTO `Reflections` (id, username, entrydate, scale, activity, description) VALUES (%s, %s, %s, %s, %s, %s)'] 

[parameters: ('username', 'entrydate', 'scale', 'activity', 'description', None)]

Here's my models.py file:

class Tasks(Base):
    __tablename__ = 'Tasks'
    id = Column(Integer, primary_key=True)
    list_id = Column(String(255), unique=False)
    username = Column(String(255), unique=False)
    title = Column(String(255), unique=False)
    description = Column(String(255), unique=False)
    created_at = Column(String(255), unique=False)
    starred = Column(String(255), unique=False)
    completed_on = Column(String(255), unique=False)


    def __init__(self, id=None, list_id=None, username=None, title=None, description=None, created_at=None, starred=None, completed_on=None):
        self.id = str(id)
        self.list_id = list_id
        self.username = username
        self.title = title
        self.description = description
        self.created_at = created_at
        self.starred = starred
        self.completed_on = completed_on


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

And my add function:

    i = Tasks('list_id', 'username', 'title', 'description', 'created_at', 'starred', 'completed_on')
    db_session.add(i)
    db_session.commit()

For some reason it's trying to insert in the 'id' column which is auto-increment. Is there a way to tell SQLAlchemy to ignore that column with inserts?

Upvotes: 0

Views: 870

Answers (2)

Yerry Aguirre
Yerry Aguirre

Reputation: 101

I believe that you are missing the id parameter. You could intent this.

i = Tasks(None, 'list_id', 'username', 'title', 'description', 'created_at', 'starred', 'completed_on')
db_session.add(i)
db_session.commit()

Upvotes: 0

danywigglebutt
danywigglebutt

Reputation: 269

Figured it out! It's because the id is defined in the __init__ method. Commenting it out/removing it like below fixed it:

class Tasks(Base):
    __tablename__ = 'Tasks'
    id = Column(Integer, primary_key=True)
    list_id = Column(String(255), unique=False)
    username = Column(String(255), unique=False)
    title = Column(String(255), unique=False)
    description = Column(String(255), unique=False)
    created_at = Column(String(255), unique=False)
    starred = Column(String(255), unique=False)
    completed_on = Column(String(255), unique=False)


    def __init__(self, id=None, list_id=None, username=None, title=None, description=None, created_at=None, starred=None, completed_on=None):
        # self.id = str(id)
        self.list_id = list_id
        self.username = username
        self.title = title
        self.description = description
        self.created_at = created_at
        self.starred = starred
        self.completed_on = completed_on


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

Upvotes: 1

Related Questions