On Mon, Mar 19, 2018 at 12:52 AM, lone ois <[email protected]> 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 [email protected].
> To post to this group, send email to [email protected].
> 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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to