Override incorrectly defined classes

36 views
Skip to first unread message

Nishant Varma

unread,
Oct 24, 2022, 12:16:10 PM10/24/22
to sqlalchemy
We use SQLAlchemy to read/write data, but not create tables (as it done by DBAs). Due to this, some of the definitions which are incorrect, have not yet been caught (though it works for reads/writes etc.).

Is there a DRY way to to override them for testing purposes (create tables on the fly etc.), without touching the original definition? A simple class-overriding doesn't seem to work, and I don't see any other solution to this problem:

MRE:

from sqlalchemy import Column, Integer, Numeric, String
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Product(Base):
    __tablename__ = "product"

    idn = Column(Numeric, primary_key=True) # should be Integer
    code = Column(String, primary_key=True) # should be unique (not primary)

class Product(Product):
    __tablename__ = "product"
    __table_args__ = {"extend_existing": True} # want to override, not extend

    idn = Column(Integer, primary_key=True)
    code = Column(String, unique=True)

For now, my plan is to create a folder(file) called backlogs(.py) and dump these classes with a new Base. I will get it corrected in due time :-).

Thanks,
Nishant

Mike Bayer

unread,
Oct 24, 2022, 2:17:21 PM10/24/22
to noreply-spamdigest via sqlalchemy
I dont think there's an obvious way to override declarative classes like that, but I also dont understand the problem such that something like that would be necessary (that is, I dont understand why you need "create tables on the fly", is this in a local testing database where you *do* create tables?  not clear).   The original Product definition you have can be used directly with an existing database table that does not have exactly the same constraints or datatypes, whether or not there is actually a PK constraint covering "code".   
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Nishant Varma

unread,
Oct 24, 2022, 3:21:09 PM10/24/22
to sqlal...@googlegroups.com
Thanks for the reply, Mike. In that case, I'll just keep a copy of our definitions in the test case folder and correct them whenever required. Hopefully, these will get merged to the real code sooner or later (which is why I wanted to name it backlogs).

The goal is to stage tables(which are required)/data(ORM-style) required for unit-testing our queries on a local SQL server. It won't involve more than 10-12 tables, usually. Such overridings also help in development/debugging (add some cool features which are not needed in production).

Regards,
Nishant 

Reply all
Reply to author
Forward
0 new messages