Sorry it took my a while to test this, but I didn't see any difference in
the SQL emitted. What did I miss?
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import UniqueConstraint
from sqlalchemy import create_engine
Base = declarative_base()
class Test1(Base):
__tablename__ = 'test_1'
id = Column(Integer, primary_key=True)
alt_id = Column(Integer, nullable=True, default=None, unique=True)
class Test2(Base):
__tablename__ = 'test_2'
id = Column(Integer, primary_key=True)
alt_id = Column(Integer, nullable=True, default=None)
__table_args__ = (UniqueConstraint('alt_id'),)
active_db_url = 'postgres://user:[email protected]/db'
engine = create_engine(active_db_url, echo=False)
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
And here's what I see in the log:
CREATE TABLE test_2 (
id SERIAL NOT NULL,
alt_id INTEGER,
PRIMARY KEY (id),
UNIQUE (alt_id)
)
REATE TABLE test_1 (
id SERIAL NOT NULL,
alt_id INTEGER,
PRIMARY KEY (id),
UNIQUE (alt_id)
)
On Sunday, May 3, 2015 at 10:23:31 PM UTC-5, Michael Bayer wrote:
>
> sure, use UniqueConstraint directly. It's better to use that than the
> unique=True flag in any case.
>
>
>
> On 5/3/15 10:29 PM, [email protected] <javascript:> wrote:
>
> Is there a way to control whether DDL emitted by SQLAlchemy uses a
> column and/or table constraint for uniqueness?
>
> It seems the following
> class Part(Base):
> __tablename__ = 'part'
> third_party_id = Column(Integer, nullable=True, default=None, unique=
> True)
>
>
> emits a table constraint
> CREATE TABLE part (
> third_party_id INTEGER,
> CONSTRAINT uq_part_third_party_id UNIQUE (third_party_id)
> )
>
>
>
>
> Is it possible to emit the following with a column constraint instead?
> CREATE TABLE part (
> third_party_id INTEGER CONSTRAINT uq_part_third_party_id UNIQUE
> )
>
> Thanks
> --
> 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] <javascript:>.
> To post to this group, send email to [email protected]
> <javascript:>.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.
>
>
>
--
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.