Difference and usage of UniqueConstraint in python class

I am using python 2.7 and sqlalchemy 0.9.9. In our code, I found various ways to define UniqueConstraint and I don't know which one is correct or if we need it at all (we don't want to create a table with our code).

Example 1:

class ExampleOne(OrmModelBase, TableModelBase):

__tablename__ = 'example_one'

example_one_id = Column(Integer, Sequence('seq_example_one_example_one_id'), 
                        primary_key=True, nullable=False)

example_one_name = Column(String, unique=True, nullable=False)

def __init__(self):
    self.example_one_id = self.getNextPkValue()
    self.example_one_name = ''

      

Example 2 (Version 1):

class ExampleTwo(OrmModelBase, TableModelBase):

__tablename__ = 'example_two'
example_two_id = Column(Integer, Sequence(
    'seq_example_two_example_two_id'), primary_key=True,
    nullable=False)

example_two_name = Column(String, nullable=False)

UniqueConstraint(
    "example_two_id", "example_two_name",
    name="uk_example_two_example_two_id_example_two_name")

def __init__(self):
    self.example_two_id = self.getNextPkValue()
    self.example_two_name = example_two_name

      

Example 2 (version 2):

class ExampleTwo(OrmModelBase, TableModelBase):

__tablename__ = 'example_two'
example_two_id = Column(Integer, Sequence(
    'seq_example_two_example_two_id'), primary_key=True,
    nullable=False)

example_two_name = Column(String, nullable=False)

uk_example_two_example_two_id_example_two_name = UniqueConstraint(
"example_two_id", "example_two_name")

def __init__(self):
    self.example_two_id = self.getNextPkValue()
    self.example_two_name = example_two_name

      

Example 3:

class ExampleThree(OrmModelBase, TableModelBase):
__tablename__ = 'example_three'

example_three_id = Column(Integer,
                          Sequence('example_three_example_three_id'),
                          primary_key=True)

example_three_name = Column(String, nullable=False)

__table_args__ = (
    UniqueConstraint("example_three_name", name="uk_example_three_name"),
)

def __init__(self):
    self.example_three_id = self.getNextPkValue()
    self.example_three_name = ""

      

I don't want to create a table with this code, I create the required tables manually with the necessary constraints.

  • When I am not creating tables, do I need unique constraint definitions (or primary_key, nullable, etc.)? Is there an effect when I write one of the three versions in a class?

I read that the first example is for unique constraints for one column and the second example is for unique constraints for more than one. But the examples I found always used a table and only one example was used in my case (classes).

  1. Is there any difference between using unique constraints and what is the difference?

I hope there is someone who can help me. Thank you for your time!

+3


source to share


1 answer


Defining unique constraints in sqlalchemy is really only used for schema generation (table creation). In addition, there are differences between the major DBMSs, how they are defined (especially in multiple columns) and what exactly is possible. This - supported by sqlalchemy - is handled dialect and more or less opaque.

So ... you wouldn't be hurt if you left them.



But.

I would leave them in the code for documentation, they can be a useful reminder of why the underlying RDBMS behaves in a certain way.

+1


source







All Articles