sqlalchemy sequence postgresql

Solutions on MaxInterview for sqlalchemy sequence postgresql by the best coders in the world

showing results for - "sqlalchemy sequence postgresql"
Laura
17 May 2019
1#!/usr/bin/env python
2
3from sqlalchemy import create_engine, Column, Integer, String, Sequence
4from sqlalchemy.ext.declarative import declarative_base
5
6Base = declarative_base()
7USER_ID_SEQ = Sequence('user_id_seq')  # define sequence explicitly
8class User(Base):
9    __tablename__ = 'users'
10    # use sequence in column definition, and pass .next_value() as server_default
11    id = Column(Integer, USER_ID_SEQ, primary_key=True, server_default=USER_ID_SEQ.next_value())
12    name = Column(String(50))
13    fullname = Column(String(50))
14    password = Column(String(12))
15
16    def __repr__(self):
17        return "<User(name='%s', fullname='%s', password='%s')>" % (
18                                self.name, self.fullname, self.password)
19
20db_url = 'postgresql://localhost/serial'
21engine = create_engine(db_url, echo=True)
22Base.metadata.create_all(engine)