On Aug 5, 2014, at 1:16 PM, dweitzenfeld <[email protected]> wrote:
> In the FAQ, in response to the Q "How do I map a table that has no primary
> key?" , it says:
>
>
> ...the best candidate key can be applied directly to the mapper:
>
> class SomeClass(Base):
> __table__ = some_table_with_no_pk
> __mapper_args__ = {
> 'primary_key':[some_table_with_no_pk.c.uid,
> some_table_with_no_pk.c.bar]
> }
> Better yet is when using fully declared table metadata, use the
> primary_key=True flag on those columns:
>
> class SomeClass(Base):
> __tablename__ = "some_table_with_no_pk"
>
> uid = Column(Integer, primary_key=True)
> bar = Column(String, primary_key=True)
>
> Two Questions:
>
> 1) Is there a way to declare a compound primary key using the fully declared
> table metadata?
>
>
The above code you have with primary_key=True on multiple columns will in fact
produce a composite primary key. Or you can create a PrimaryKeyConstraint()
with the columns you want marked primary. There's background on this from a
core perspective at
http://docs.sqlalchemy.org/en/rel_0_9/core/constraints.html?highlight=primarykeyconstraint#sqlalchemy.schema.PrimaryKeyConstraint.
> 2) Why, in a nutshell, is using a fully declared table metadata is "better"?
>
>
When Table/Column know about the primary key, SQLAlchemy can make better
decisions about dealing with auto generation of primary key values, if
applicable. Compatibility with the database's auto increment and/or
sequencing capabilities occurs at the level of Core (e.g. Table/Column, not
mapping).
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.