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
>
> 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 [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sqlalchemy/71fd3daa-001b-4359-915b-d4156ed21878%40googlegroups.com
>
> <https://groups.google.com/d/msgid/sqlalchemy/71fd3daa-001b-4359-915b-d4156ed21878%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 [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/a81c9b4a-7040-4c94-aa21-468bbe5a1a7b%40www.fastmail.com.