Implementing archive deletion for models with relationships

15 views
Skip to first unread message

Kata Char

unread,
Jul 30, 2019, 3:42:49 PM7/30/19
to sqlalchemy
Hi,

I was wondering if I can get some pointers on how to do this. I want to copy data to an archive table before deleting the data. So let's say I have a model like below

class ModelA(...):
   id
= db.Column(db.Integer, primary_key=True)
   fake_model_id
= db.Column(db.Integer,db.ForeignKey('some_fake_model.id'))
   
...

   fake
= db.relationship('SomeFakeModel')
   
...

For the archived class, do I need to include the relationship? The reason I ask is because I'm not sure how the relationship is populated via sqlalchemy.

So for the use case:
   1. delete ModelA
   2. restore ModelA

class ArchivedModelA(...):
   id
= db.Column(db.Integer, primary_key=True)
   fake_model_id
= db.Column(db.Integer,db.ForeignKey('some_fake_model.id'))
   
...
   
# no relationship here - is it needed?
   
...

To restore ModelA, I'd copy ArchivedModelA attributes to ModelA (e.g https://stackoverflow.com/a/36225970) - but if ArchivedModelA doesn't have the "fake" relationship, is the new restored ModelA going to have the fake relationship via the foreign key? The 
.columns.keys()
 doesn't return the relationship.

Hope what I wrote makes sense!

Mike Bayer

unread,
Aug 3, 2019, 11:25:54 PM8/3/19
to noreply-spamdigest via sqlalchemy


On Tue, Jul 30, 2019, at 3:42 PM, Kata Char wrote:
Hi,

I was wondering if I can get some pointers on how to do this. I want to copy data to an archive table before deleting the data. So let's say I have a model like below

class ModelA(...):
   id
= db.Column(db.Integer, primary_key=True)
   fake_model_id
= db.Column(db.Integer,db.ForeignKey('some_fake_model.id'))
   
...

   fake
= db.relationship('SomeFakeModel')
   
...

For the archived class, do I need to include the relationship? The reason I ask is because I'm not sure how the relationship is populated via sqlalchemy.

you don't need the relationship unless you want to use it in Python as part of how you copy data.   you can copy data from ModelA.fake_model_id to ArchivedModelA.fake_model_id directly.



So for the use case:
   1. delete ModelA
   2. restore ModelA

class ArchivedModelA(...):
   id
= db.Column(db.Integer, primary_key=True)
   fake_model_id
= db.Column(db.Integer,db.ForeignKey('some_fake_model.id'))
   
...
   
# no relationship here - is it needed?
   
...

To restore ModelA, I'd copy ArchivedModelA attributes to ModelA (e.g https://stackoverflow.com/a/36225970) - but if ArchivedModelA doesn't have the "fake" relationship, is the new restored ModelA going to have the fake relationship via the foreign key? The 
.columns.keys()
 doesn't return the relationship.

the best way to get an answer to things like this is to just try it!    relationship() is not to copy data between tables if you are copying the values of all the foreign key columns directly.




Hope what I wrote makes sense!


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

Reply all
Reply to author
Forward
0 new messages