On Wed, Apr 25, 2018 at 3:04 PM, Stanislav Lobanov <n10101...@gmail.com> wrote:
> Hello!
>
> I have a base model class with "id" field, which is primary key and also not
> nullable. It does not have autoincrement feature, but uses custom sequence
> manually to assign it's value.
>
> There are standard on table / sequence naming:
>
> class Base:
>     id = Column(Integer, primary_key=True, nullable=False)

>
>
> class A(Base):
>     __tablename__ = 'a'
>

to use Declarative with common attributes, you need to use either an
__abstract__ base class or a mixin, and then you need to extend from
declarative_base(). This is probably what you're doing, but since that
is not shown I have to make sure this is known; see
http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/mixins.html


>
> class B(Base):
>     __tablename__ = 'b'
>
>
>
> What i need is to do two things:
>
> 1. Automatically add sequences to A and B tables (in ORM context, mappers),
> named "__tablename__$seq" -> 'a$seq' and 'b$seq'. By sequences i mean an
> attribute of type Sequence.
> 2. Somehow associate it with "id" column, so the sequence can be
> automatically created after "create_all()" function call.
>
> I tried to use this approach: Associating a Sequence as the Server Side
> Default
> (http://docs.sqlalchemy.org/en/latest/core/defaults.html#associating-a-sequence-as-the-server-side-default)
> but did not succeed because sequence name is always hardcoded in base class,
> but in my case it must be autogenerated based on custom naming rule.

whether or not you add the sequence as a server side default isn't
that important, but setting up the Sequence for the column is most
easily achieved by having it inline with the column:

Column(Integer, Sequence("foo_seq"))

then to create the name dynamically, you need to use declared_attr,
again this is in
http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative/mixins.html#mixing-in-columns:

class Base(Base):
    __abstract__ = True

    @declared_attr
    def id(cls):
        return Column(
            Integer, Sequence("%s_seq" % cls.__tablename__),
            primary_key=True, nullable=False)


if you want to add server_default that would also be within the given
id() method.



>
> I have spent four hours trying different techniques but did not succeed with
> this.
>
> Thanks!
>
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full
> description.
> ---
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to