我们从Python开源项目中,提取了以下34个代码示例,用于说明如何使用sqlalchemy.Sequence()。
def info_table(base): class Info(base): __tablename__ = 'Info' id_ = Column(Integer, Sequence('trigger_id_seq'), primary_key=True) run_id=Column(String, nullable=False) name=Column(String, nullable=False) value=Column(String, nullable=True) __table_args__ = ( UniqueConstraint('run_id', 'name'), ) def __repr__(self): return "<Info(run_id='%s', name='%s', value='%s')>" % (self.run_id, self.name, self.value) return Info
def upgrade(): op.execute(sa.schema.CreateSequence(sa.Sequence('pod_states_id_seq'))) op.add_column('pod_states', sa.Column('id', sa.Integer(), nullable=False, server_default=sa.text("nextval('pod_states_id_seq'::regclass)"))) op.execute("ALTER TABLE pod_states DROP CONSTRAINT pod_states_pkey, " "ADD CONSTRAINT pod_states_pkey PRIMARY KEY (id);") op.add_column('container_states', sa.Column('exit_code', sa.Integer(), nullable=True)) op.add_column('container_states', sa.Column('pod_state_id', sa.Integer(), nullable=True)) op.add_column('container_states', sa.Column('reason', sa.Text(), nullable=True)) op.create_index('ix_pod_id_start_time', 'pod_states', ['pod_id', 'start_time'], unique=True) op.create_foreign_key('container_states_pod_state_id_fkey', 'container_states', 'pod_states', ['pod_state_id'], ['id']) upgrade_data() op.alter_column('container_states', 'pod_state_id', existing_type=sa.INTEGER(), nullable=False) op.drop_constraint(u'container_states_pod_id_fkey', 'container_states', type_='foreignkey') op.drop_column('container_states', 'pod_id')
def downgrade(): op.add_column('container_states', sa.Column('pod_id', postgresql.UUID(), autoincrement=False, nullable=True)) op.create_foreign_key(u'container_states_pod_id_fkey', 'container_states', 'pods', ['pod_id'], ['id']) downgrade_data() op.drop_column('container_states', 'reason') op.drop_column('container_states', 'exit_code') op.drop_constraint('container_states_pod_state_id_fkey', 'container_states', type_='foreignkey') op.drop_index('ix_pod_id_start_time', table_name='pod_states') op.drop_column('container_states', 'pod_state_id') op.execute("ALTER TABLE pod_states DROP CONSTRAINT pod_states_pkey, " "ADD CONSTRAINT pod_states_pkey PRIMARY KEY (pod_id, start_time);") op.drop_column('pod_states', 'id') op.execute(sa.schema.DropSequence(sa.Sequence('pod_states_id_seq')))
def trigger_table(base): class Trigger(base): __tablename__ = 'Trigger' id_ = Column(Integer, Sequence('trigger_id_seq'), primary_key=True) run_id = Column(String, nullable=False, default='') event_id = Column(String, nullable=False) sequence = Column(Integer, nullable=False) recovery = Column(Integer, nullable=False, default=0) created = Column(DateTime(), default=datetime.utcnow) acted = Column(DateTime(), nullable=True) def __repr__(self): return "<Trigger(id='%s', run_id='%s', event_id='%s', sequence='%s', recovery='%s', created='%s', acted='%s')>" % ( self.id_, self.run_id, self.event_id, self.sequence, self.recovery, self.created, self.acted) return Trigger
def task_table(base): class Task(base): __tablename__ = 'Task' id_ = Column(Integer, Sequence('task_id_seq'), primary_key=True) run_id = Column(String, default='') step_id = Column(String,) sequence = Column(Integer,) recovery = Column(Integer, nullable=False) pid = Column(Integer, nullable=True) status = Column(SQLEnum(TaskStatus), ) result = Column(PickleType() , nullable=True,) created = Column(DateTime(), default=datetime.utcnow) updated = Column(DateTime(), nullable=True, ) __table_args__ = ( UniqueConstraint('run_id', 'step_id', 'sequence', 'recovery'), ) def __repr__(self): return "<Task(id='%s', run_id='%s', step_id='%s', sequence='%s', recovery='%s', pid='%s', status='%s', created='%s', updated='%s')>" % ( self.id_, self.run_id, self.step_id, self.sequence, self.recovery, self.pid, self.status, self.created, self.updated) return Task
def delay_table(base): class Delay(base): __tablename__ = 'Delay' id_=Column(Integer, Sequence('delay_id_seq'), primary_key=True) run_id = Column(String, default='') delay_id = Column(String,) sequence = Column(Integer,) recovery = Column(Integer,) seconds = Column(BigInteger, nullable=True) active = Column(Boolean, default=False) activated = Column(DateTime(), nullable=True,) updated = Column(DateTime(), default=datetime.utcnow) __table_args__ = ( UniqueConstraint('run_id', 'delay_id', 'sequence', 'recovery'), ) def __repr__(self): return "<Delay(id='%s', run_id='%s', delay_id='%s', delay='%s', active='%s', activated='%s')>" % ( self.id_, self.run_id, self.delay_id, self.seconds, self.active, self.activated) return Delay
def AutoIncrementColumn(type_, **kwargs): try: sequence_name = kwargs.pop('sequence_name') except KeyError: raise TypeError('AutoIncrementColumn needs a `sequence_name`') kwargs.update( server_default=sqlalchemy.text("nextval('%s')" % sequence_name), nullable=False, unique=True, ) col = Column(type_, **kwargs) def after_parent_attach(target, parent): sequence = Sequence(sequence_name) sqlalchemy.event.listen(col.table, 'before_create', CreateSequence(sequence)) sqlalchemy.event.listen(col.table, 'after_drop', DropSequence(sequence)) sqlalchemy.event.listen(col, 'after_parent_attach', after_parent_attach) return col
def get_column_specification(self, column, **kwargs): colspec = ( self.preparer.format_column(column) + " " + self.dialect.type_compiler.process( column.type, type_expression=column) ) if column.nullable is not None: if not column.nullable or column.primary_key or \ isinstance(column.default, sa_schema.Sequence): colspec += " NOT NULL" else: colspec += " NULL" if column.table is None: raise exc.CompileError( "mssql requires Table-bound columns " "in order to generate DDL") # install an IDENTITY Sequence if we either a sequence or an implicit # IDENTITY column if isinstance(column.default, sa_schema.Sequence): if column.default.start == 0: start = 0 else: start = column.default.start or 1 colspec += " IDENTITY(%s,%s)" % (start, column.default.increment or 1) elif column is column.table._autoincrement_column: colspec += " IDENTITY(1,1)" else: default = self.get_column_default_string(column) if default is not None: colspec += " DEFAULT " + default return colspec
def get_column_specification(self, column, **kwargs): colspec = (self.preparer.format_column(column) + " " + self.dialect.type_compiler.process(column.type)) if column.nullable is not None: if not column.nullable or column.primary_key or \ isinstance(column.default, sa_schema.Sequence): colspec += " NOT NULL" else: colspec += " NULL" if column.table is None: raise exc.CompileError( "mssql requires Table-bound columns " "in order to generate DDL") # install an IDENTITY Sequence if we either a sequence or an implicit # IDENTITY column if isinstance(column.default, sa_schema.Sequence): if column.default.start == 0: start = 0 else: start = column.default.start or 1 colspec += " IDENTITY(%s,%s)" % (start, column.default.increment or 1) elif column is column.table._autoincrement_column: colspec += " IDENTITY(1,1)" else: default = self.get_column_default_string(column) if default is not None: colspec += " DEFAULT " + default return colspec
def get_column_specification(self, column, **kwargs): colspec = (self.preparer.format_column(column) + " " + self.dialect.type_compiler.process(column.type)) if column.nullable is not None: if not column.nullable or column.primary_key or \ isinstance(column.default, sa_schema.Sequence): colspec += " NOT NULL" else: colspec += " NULL" if column.table is None: raise exc.CompileError( "mssql requires Table-bound columns " "in order to generate DDL") # install an IDENTITY Sequence if we either a sequence or an implicit IDENTITY column if isinstance(column.default, sa_schema.Sequence): if column.default.start == 0: start = 0 else: start = column.default.start or 1 colspec += " IDENTITY(%s,%s)" % (start, column.default.increment or 1) elif column is column.table._autoincrement_column: colspec += " IDENTITY(1,1)" else: default = self.get_column_default_string(column) if default is not None: colspec += " DEFAULT " + default return colspec
def upgrade(): # Had to add this explicitly, because of the use of Sequence() in the SQLAlchemy model without # setting server default op.alter_column('callset', 'id', server_default=sa.Sequence('callset_id_seq').next_value()) op.create_table('sample', sa.Column('id', sa.BigInteger, nullable=False), sa.Column('guid', sa.String(length=36), nullable=False), sa.Column('individual_id', sa.BigInteger, nullable=False), sa.Column('name', sa.Text(), nullable=False), sa.Column('info', TextPickleType, nullable=True), sa.ForeignKeyConstraint(['individual_id'], ['individual.id'],), sa.PrimaryKeyConstraint('id'), sa.UniqueConstraint('guid') ) op.create_table('callset_to_db_array_association', sa.Column('callset_id', sa.BigInteger, nullable=False), sa.Column('db_array_id', sa.BigInteger, nullable=False), sa.Column('tile_row_id', sa.BigInteger, nullable=True), sa.ForeignKeyConstraint(['callset_id'], ['callset.id'],), sa.ForeignKeyConstraint(['db_array_id'], ['db_array.id'],), sa.PrimaryKeyConstraint('callset_id', 'db_array_id', name='primary_key')) op.create_index( 'db_array_id_tile_row_id_idx', 'callset_to_db_array_association',['db_array_id', 'tile_row_id'], unique=True) # Trigger for auto-incrementing tile_row_idx and num_rows op.execute('''\ CREATE OR REPLACE FUNCTION increment_num_rows_in_db_array_pgsql() RETURNS trigger AS $increment_num_rows_in_db_array_pgsql$ BEGIN UPDATE callset_to_db_array_association SET tile_row_id=(select num_rows from db_array where id=NEW.db_array_id) where NEW.tile_row_id IS NULL and db_array_id=NEW.db_array_id and callset_id=NEW.callset_id; UPDATE db_array SET num_rows=num_rows+1 WHERE id = NEW.db_array_id; RETURN NEW; END; $increment_num_rows_in_db_array_pgsql$ LANGUAGE plpgsql; CREATE TRIGGER increment_num_rows_in_db_array AFTER INSERT ON callset_to_db_array_association FOR EACH ROW EXECUTE PROCEDURE increment_num_rows_in_db_array_pgsql(); ''') op.add_column(u'callset', sa.Column('source_sample_id', sa.BigInteger, nullable=False)) op.add_column(u'callset', sa.Column('target_sample_id', sa.BigInteger, nullable=False)) op.drop_constraint(u'callset_dbrow_id_fkey', 'callset', type_='foreignkey') op.drop_constraint(u'callset_individual_id_fkey','callset', type_='foreignkey') op.create_foreign_key('callset_source_sample_id_fkey','callset', 'sample', ['source_sample_id'], ['id']) op.create_foreign_key('callset_target_sample_id_fkey','callset', 'sample', ['target_sample_id'], ['id']) op.drop_column(u'callset', 'dbrow_id') op.drop_column(u'callset', 'individual_id') op.add_column(u'db_array', sa.Column('num_rows', sa.BigInteger, nullable=False)) op.drop_table('db_row')