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".    

On Mon, Oct 24, 2022, at 12:16 PM, Nishant Varma wrote:
> 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
> 
> 
> -- 
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
>  
> http://www.sqlalchemy.org/
>  
> 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+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/00773483-19c1-482f-9743-9e318651b724n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/00773483-19c1-482f-9743-9e318651b724n%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

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+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/aa577392-2c3e-4c18-b539-886743a5f0f6%40app.fastmail.com.

Reply via email to