Mike thank you. I think, Use __table_args__ This is the definitive usage. If you can add a sample to the Sqlalchemy documentation. Let beginners avoid using the wrong method and confused.
Mike Bayer於 2018年3月19日星期一 UTC+8下午9時27分47秒寫道: > > On Mon, Mar 19, 2018 at 12:52 AM, lone ois <ois...@gmail.com <javascript:>> > wrote: > > Hi! > > i test sqlalchemy 1.1.12 - 1.2.5 > > create table check constraint. > > ----------------------------------- > > class testTable(db): > > __tablename__ = 'testTable' > > > > id = Column(INT, primary_key = True, autoincrement = True, unique = > > True) > > ser = Column(INT) > > > > CheckConstraint('ser>100') > > > > #sqlalchemy echo and pgadmin4 > > > > CREATE TABLE "testTable" ( > > id SERIAL NOT NULL, > > ser INTEGER, > > PRIMARY KEY (id), > > UNIQUE (id) > > > > no check constraint. > > ------------------------------ > > class testTable(db): > > __tablename__ = 'testTable' > > > > id = Column(INT, primary_key = True, autoincrement = True, unique = > > True) > > ser = Column(INT) > > __table_args__ = ( > > CheckConstraint('ser>100'), > > ) > > > > > > #sqlalchemy echo > > CREATE TABLE "testTable" ( > > id SERIAL NOT NULL, > > ser INTEGER, > > PRIMARY KEY (id), > > CHECK (ser>100), > > UNIQUE (id) > > > > this OK! > > > > so, i must use __table_args__ define CheckConstraint ?? > > when you add an object to a Python class: > > class MyClass(object): > > # .... > > "hello!" > > that string "hello" has no way of being intercepted by the class and > it knows nothing about it. SQLAlchemy's declarative has no way to see > this. So with your CheckConstraint example, this suggests that yes, > you do need to use __table_args__. However, there is an exception, > which is probably what you discovered, if you make a UniqueConstraint > or CheckConstraint that refers to the Column objects directly, > SQLAlchemy figures this out: > > > class MyClass(Base): > # ... > foo = Column(...) > bar = Column(...) > > CheckConstraint("foo > bar") > > vs. > > class MyClass(Base): > # ... > foo = Column(...) > bar = Column(...) > > CheckContraint(foo > bar) > > > That said, you should really use __table_args__ for these as that is > the idiomatic approach and will work consistently in all cases. > > > > > > > > > > > > > > > > -- > > 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+...@googlegroups.com <javascript:>. > > To post to this group, send email to sqlal...@googlegroups.com > <javascript:>. > > 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.