Pat R Ellery Pat R Ellery - 1 year ago 140
Python Question

sqlalchemy date error. Argument 'arg' is expected STR but got INT

I have a database schema (which i cannot change) with dates. they are defined as:

| Field | Type | Null | Key | Default | Extra |
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| access_date | int(10) unsigned | NO | | 0 | |

now, my model as this as defined:

from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

class logme(Base):

id_ = Column(Integer, primary_key=True)
access_date = Column(Integer, nullable=False, server_default=0)

When i load the model i get this error:

sqlalchemy.exc.ArgumentError: Argument 'arg' is expected to be one of type '<class 'str'>' or '<class 'sqlalchemy.sql.elements.ClauseElement'>' or '<class 'sqlalchemy.sql.elements.TextClause'>', got '<class 'int'>'

if I comment out the access_date everything works fine

Answer Source

Maybe this will help? (

Scalar Defaults

The simplest kind of default is a scalar value used as the default value of a column:

Table("mytable", meta, Column("somecolumn", Integer, default=12) ) Above, the value “12” will be bound as the column value during an INSERT if no other value is supplied.

You are using server_default parameter


which is explained here

Server Side Defaults¶

A variant on the SQL expression default is the server_default, which gets placed in the CREATE TABLE statement during a create() operation:

t = Table('test', meta, Column('abc', String(20), server_default='abc'), Column('created_at', DateTime, server_default=text("sysdate")) ) A create call for the above table will produce:

CREATE TABLE test ( abc varchar(20) default 'abc', created_at datetime default sysdate ) The behavior of server_default is similar to that of a regular SQL default; if it’s placed on a primary key column for a database which doesn’t have a way to “postfetch” the ID, and the statement is not “inlined”, the SQL expression is pre-executed; otherwise, SQLAlchemy lets the default fire off on the database side normally.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download