Pylons, SQlite and Auto-Increment Fields

Hey! Just started working with Pylons in combination with SQLAlchemy, and my model looks something like this:

from sqlalchemy import Column from sqlalchemy.types import Integer, String from helloworld.model.meta import Base class Person(Base): __tablename__ = "person" id = Column(Integer, primary_key=True) name = Column(String(100)) email = Column(String(100)) def __init__(self, name='', email=''): self.name = name self.email = email def __repr__(self): return "<Person('%s')" % self.name 

In order to avoid reusing the sqlite identifier that might have been deleted, I want to add AUTOINCREMENT to the id column. I looked through the sqlalchemy documentation and saw that sqlite_autoincrement could be released. An example where this attribute is specified can be found here .

sqlite_autoincrement seems to be released when the table itself was created, and I just wondered how it could be provided using a declarative style model like mine.

+6
sqlite sqlalchemy auto-increment pylons
source share
1 answer

Try to include the __table_args__ attribute with the arguments that you pass to the Table constructors in the traditional (non-declarative) style of the data definition, for example:

 class Person(Base): __tablename__ = "person" __table_args__ = {'sqlite_autoincrement': True} 

If you need to include multiple arguments, use this form (dict must be the last):

 __table_args__ = ( Unique('foo'), # ... {'sqlite_autoincrement': True} ) 

In the Configuration Table section of declarative SQLAlchemy documentation:

Table arguments other than Column names, metadata, and associated arguments are specified using the __table_args__ class __table_args__ . This attribute contains both positional and keyword arguments, which are usually sent to the Table constructor.

+17
source share

All Articles