Required SQLAlchemy table properties

When writing SQLAlchemy models for an existing database, how much table information do I need to give? Consider this table, which is part of the MySQL database:

CREATE TABLE entities (
    id INTEGER NOT NULL AUTO_INCREMENT,
    dn VARCHAR(100) NOT NULL UNIQUE,
    PRIMARY KEY (id)
) Engine=InnoDB, COLLATE utf8_unicode_ci;

Based on my testing, this would be enough to use it:

class Entity(Base):
    __tablename__ = 'entities'
    id          = Column('id', Integer, primary_key=True)
    dn          = Column('dn', String(100))

But, of course, it lacks UNIQUE, AUTO_INCREMENT, Engine, and COLLATE information. Does SQLAlchemy mean this data?

Of course, I could use Reflection, but I would prefer not because of consistency.

+5
source share
1 answer

Below is the semantic result, but will not produce the same query:

class Entity(Base):
    __tablename__ = 'entities'
    __table_args__ = {'mysql_engine':'InnoDB'}
    id          = Column('id', Integer, primary_key=True)
    dn          = Column('dn', String(100, collation='utf8_unicode_ci'), unique=True)

I am not sure how to specify sorting for the whole table.

+4
source

All Articles