-1

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

  1. 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?
  2. ingest_complete has a nullable=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?
  3. 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 the UniqueConstraint doesn't seem to be used outside of Alembic schema management.
medley56
  • 1,181
  • 1
  • 14
  • 29
  • Hey mods, I'm interested in getting this question into a better form. I've made some edits. If you still think it's not focused enough, please give me a bit of guidance on what I can do to improve it. – medley56 Aug 23 '20 at 17:24

1 Answers1

1

That data is unnecessary, because the database you are using has a schema that is separate from your python code. The database schema is managed externally by a tool which generates it from your python code. So you don't need your python code to have all the information that the database schema has.

Ido Nov
  • 11
  • 3
  • Succinct, clear, and it happens to line up with what I thought. – medley56 Aug 22 '20 at 22:46
  • I've "un-accepted" this answer following the voted closure of my question. Now that the question is more focused, it will need a more specific answer. @Ido Nov, feel free to add more specificity. – medley56 Aug 23 '20 at 17:31