Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

About unique=True and (unique=True, index=True) in sqlalchemy

When I create tables use flask-sqlalchemy like this:

class Te(Model):
    __tablename__ = 'tt'
    id = Column(db.Integer(), primary_key=True)
    t1 = Column(db.String(80), unique=True, )
    t3 = Column(db.String(80), unique=True, index=True, )

and In my Sequel Pro , I get the table create info:

CREATE TABLE `tt` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `t1` varchar(80) DEFAULT NULL,
  `t3` varchar(80) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `t1` (`t1`),
  UNIQUE KEY `ix_tt_t3` (`t3`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

this means t1 is entirely same as t3 in MySQL? So when you define unique=True, it's not require to define index=True?

Thanks.

like image 934
hjlarry Avatar asked Sep 01 '18 05:09

hjlarry


People also ask

What is index true in SQLAlchemy?

Introduction to SQLAlchemy index. SQLAlchemy Index is used for assigning the identifiers for each of the particular row getting stored inside a table. We can have indexing based on the single column or collection of two or more columns together acting as an index to the table rows.

What is unique in SQLAlchemy?

UNIQUE Constraint Unique constraints can be created anonymously on a single column using the unique keyword on Column . Explicitly named unique constraints and/or those with multiple columns are created via the UniqueConstraint table-level construct.

Does unique constraint create index?

Yes, absolutely. A unique constraint creates a unique index.

What is lazy true in SQLAlchemy?

Lazy parameter determines how the related objects get loaded when querying through relationships. Below listed are the four main lazy parameters. Typically when you query the database, the data get loaded at once; however, lazy parameter allows you to alternate the way they get loaded. lazy = 'select' (or True)

Is an index required in SQLAlchemy?

The way how sqlalchemy nouns the index is idx_%columnlabbel. And that matches with the sql code generated. So the use or not of index it is only related with performance and the unique key means that the column values cannot be repeated all along of the same column in the 'tt' table. Show activity on this post. It is not required.

How to rename Index in SQLAlchemy?

You may also want to check out all available functions/classes of the module sqlalchemy , or try the search function . def rename_index(index, name, table=None, engine=None, **kw): """Rename an index. If Index instance is given, table and engine are not used.

Is a unique constraint required to implement a unique index?

It is not required. A unique constraint is more often than not implemented using a unique index, but you need not care about that detail, if all you want is uniqueness. Thanks for contributing an answer to Stack Overflow!

How are case insensitive names specified in SQLAlchemy?

Case insensitive names are specified in SQLAlchemy only by stating the name with all lower case characters. quote_schema ¶ – same as ‘quote’ but applies to the schema identifier. The schema name for this table, which is required if the table resides in a schema other than the default selected schema for the engine’s database connection.


2 Answers

I think you have a term confusion with the index purpose in sqlalchemy. In sql databases index are used speed up query performance.

According to the sqlalchemy documentation of defining constraints and indexes.

You would notice the use of the index key because the sql code generated is:

UNIQUE KEY `ix_tt_t3` (`t3`)

The way how sqlalchemy nouns the index is idx_%columnlabbel. And that matches with the sql code generated.

So the use or not of index it is only related with performance and the unique key means that the column values cannot be repeated all along of the same column in the 'tt' table.

Hope this helps,

like image 99
LucasPC Avatar answered Oct 13 '22 02:10

LucasPC


It is not required. A unique constraint is more often than not implemented using a unique index, but you need not care about that detail, if all you want is uniqueness.

like image 33
Ilja Everilä Avatar answered Oct 13 '22 03:10

Ilja Everilä