<p dir="auto"><strong>Migrated issue, originally created by Alistair Watson</strong></p>
<p dir="auto">The code below emits:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="CREATE SEQUENCE test_sequence"><pre class="notranslate"><code class="notranslate">CREATE SEQUENCE test_sequence
</code></pre></div>
<p dir="auto">when according to the documentation it should get it's default schema value from the metadata and it should emit:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="CREATE SEQUENCE test_schema.test_sequence"><pre class="notranslate"><code class="notranslate">CREATE SEQUENCE test_schema.test_sequence
</code></pre></div>
<p dir="auto">It's not really an issue if you are connecting as the user/schema you want to own the sequence - unfortunately I need to create all tables and database objects as a different user. Providing the schema parameter to each sequence directly does solve the issue.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="from sqlalchemy import create_engine, MetaData, Column, Integer,Sequence
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('oracle+cx_oracle://test_schema:Welcome1@localhost:1521/xe', echo=True)
metadata = MetaData(schema='test_schema')
Base = declarative_base(metadata=metadata)

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, Sequence(name=&quot;test_sequence&quot;), primary_key=True)
    data = Column(Integer)
    
Base.metadata.drop_all(engine, checkfirst = True)    
Base.metadata.create_all(engine)"><pre class="notranslate"><code class="notranslate">from sqlalchemy import create_engine, MetaData, Column, Integer,Sequence
from sqlalchemy.ext.declarative import declarative_base

engine = create_engine('oracle+cx_oracle://test_schema:Welcome1@localhost:1521/xe', echo=True)
metadata = MetaData(schema='test_schema')
Base = declarative_base(metadata=metadata)

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, Sequence(name="test_sequence"), primary_key=True)
    data = Column(Integer)
    
Base.metadata.drop_all(engine, checkfirst = True)    
Base.metadata.create_all(engine)
</code></pre></div>
<p dir="auto"><strong>Migrated issue, originally created by Andrew Paulo Robillo (<a href="https://github.com/shimofuri">@shimofuri</a>)</strong></p>
<p dir="auto">I'd been copying this <a href="https://bitbucket.org/snippets/shimofuri/bALAz" rel="nofollow">snippet</a> from my old cookbook to add a surrogate key to a declarative table class:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="class MixinIntKey(object):
    @declared_attr
    def key(cls):
        return Column(
            pg.INTEGER,
            Sequence(f&quot;sqk_{cls.__tablename__}&quot;, 1, 1),
            nullable=False,
            primary_key=True
            )"><pre class="notranslate"><code class="notranslate">class MixinIntKey(object):
    @declared_attr
    def key(cls):
        return Column(
            pg.INTEGER,
            Sequence(f"sqk_{cls.__tablename__}", 1, 1),
            nullable=False,
            primary_key=True
            )
</code></pre></div>
<p dir="auto">This code was working before (some 3-4 years ago, I think around version 0.8 ): it generates the Sequence when I build the database objects through metadata.create_all(). The only change I made to the snippet was to update it to use format strings (Python 3.6).</p>
<p dir="auto">After some googling around and testing (see attached file/<a href="https://bitbucket.org/snippets/shimofuri/bALAz" rel="nofollow">link</a>), the Sequence can be generated only if the metadata is passed during Sequence init:</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Sequence(f&quot;sqk_{cls.__tablename__}&quot;, 1, 1, metadata=metadata)"><pre class="notranslate"><code class="notranslate">Sequence(f"sqk_{cls.__tablename__}", 1, 1, metadata=metadata)
</code></pre></div>
<p dir="auto">My environment: psycopg2==2.7.1, SQLAlchemy==1.1.8, Python 3.6 64bit on Windows 8.1 64bit, PostgreSQL 9.6 server</p>
<hr>
<p dir="auto">Attachments: <a href="../wiki/imported_issue_attachments/3951/sequence_bug.py">sequence_bug.py</a></p>