I'm working with a database where we have elected to manage the schema with external tools and are moving away from python for schema management. Our SQLALchemy ORM models have stuck around though of course and I'm now wondering if much of the information included with them is effectively cruft in our codebase. We have comments, constraints, indices (some of which are no longer in sync) that are defined on the models. Is this information used in any way when making queries?
Edit:
The community has made it clear (with good reason) that this question needs to be more directed, so here's a specific example of a Table model and some specific "subquestions".
class FileRecord(Base):
__tablename__ = 'science_file'
# Table fields
file_id = Column(Integer, primary_key=True, autoincrement=True)
filename = Column(String, nullable=False, unique=True,
comment='File name, unique by date and version')
file_date = Column(DateTime,
comment='Date parsed from file name')
file_version = Column(Integer,
comment='Version parsed from file name')
ingest_complete = Column(Boolean, nullable=False, default=False,
comment='All packets in the file successfully ingested')
ingested_at = Column(DateTime, default=datetime.utcnow,
comment='Timestamp when file was ingested')
# Relationships
science_packets = relationship("SciencePacket",
secondary="sci_pkt_jt", backref=backref("files", lazy='joined'))
stim_packets = relationship("StimPacket",
secondary="stim_pkt_jt", backref=backref("files", lazy='joined'))
__table_args__ = (UniqueConstraint('file_date', 'file_version'),)
Questions
- The
__table_args__
contains a unique constraint (could also contain an index e.g.). Is this used when manipulating object instances or is this only useful if managing the database with SQLAlchemy? ingest_complete
has anullable=False
constraint in the model. Is this necessary if the database enforces it? Is there "pre-checking" done on the model before attempting to persist an instance or is this syntax only useful if managing a database with SQLAlchemy/Alembic?- Is there a more general rule for which parts of the model definition are/not useful? e.g.
primary_key
is definitely necessary in the model definition but theUniqueConstraint
doesn't seem to be used outside of Alembic schema management.