From c5e62914b698bad3121c436ce7ebc804af2f7429 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Wed, 21 Jul 2021 15:02:23 +0200 Subject: [PATCH] try solve problems with foreignkey --- .../versions/7ecb8ff7abad_documents.py | 6 ++-- ereuse_devicehub/resources/action/models.py | 28 ++++++++++++------- ereuse_devicehub/resources/action/schemas.py | 2 ++ .../resources/action/views/documents.py | 9 ++++-- .../resources/documents/models.py | 2 +- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/ereuse_devicehub/migrations/versions/7ecb8ff7abad_documents.py b/ereuse_devicehub/migrations/versions/7ecb8ff7abad_documents.py index 163da39b..9b018f04 100644 --- a/ereuse_devicehub/migrations/versions/7ecb8ff7abad_documents.py +++ b/ereuse_devicehub/migrations/versions/7ecb8ff7abad_documents.py @@ -29,6 +29,7 @@ def get_inv(): raise ValueError("Inventory value is not specified") return INV + def upgrade(): # Document table op.create_table('document', @@ -58,16 +59,15 @@ def upgrade(): # ToErased table op.create_table('to_erased', - # sa.Column('document_id', sa.BigInteger(), nullable=True), + sa.Column('document_id', sa.BigInteger(), nullable=False), sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), - # sa.ForeignKeyConstraint(['document_id'], [f'{get_inv()}.document.id'], ), + sa.ForeignKeyConstraint(['document_id'], [f'{get_inv()}.document.id'], ), sa.ForeignKeyConstraint(['id'], [f'{get_inv()}.action.id'], ), sa.PrimaryKeyConstraint('id'), schema=f'{get_inv()}' ) - def downgrade(): op.drop_table('to_erased', schema=f'{get_inv()}') op.drop_table('document', schema=f'{get_inv()}') diff --git a/ereuse_devicehub/resources/action/models.py b/ereuse_devicehub/resources/action/models.py index f9dfd45d..81e08074 100644 --- a/ereuse_devicehub/resources/action/models.py +++ b/ereuse_devicehub/resources/action/models.py @@ -1332,16 +1332,24 @@ class ToErased(ActionWithMultipleDevices): """ document_comment = """The user that gets the device due this deal.""" # document_id = db.Column(BigInteger, - # db.ForeignKey('document.id', - # use_alter=True, - # name='document'), - # nullable=False) - # document = relationship('EraseDocument', - # backref=backref('actions', - # lazy=True, - # uselist=False, - # cascade=CASCADE_OWN), - # primaryjoin='ToErased.document_id == EraseDocument.id') + # db.ForeignKey('document.id'), + # nullable=False) + # document = db.relationship('EraseDocument', + # backref=backref('actions', + # # lazy=True, + # # uselist=False, + # # cascade=CASCADE_OWN), + # uselist=True, + # lazy=True, + # order_by=lambda: Action.end_time, + # collection_class=list), + # primaryjoin='ToErased.document_id == EraseDocument.id') + document_id = Column(BigInteger, ForeignKey('document.id'), nullable=False) + document = relationship('Document', + backref=backref('document_one', + lazy=True, + cascade=CASCADE_OWN), + primaryjoin='ToErased.document_id == Document.id') class Prepare(ActionWithMultipleDevices): diff --git a/ereuse_devicehub/resources/action/schemas.py b/ereuse_devicehub/resources/action/schemas.py index f667eebc..1c2eff1b 100644 --- a/ereuse_devicehub/resources/action/schemas.py +++ b/ereuse_devicehub/resources/action/schemas.py @@ -432,6 +432,8 @@ class Prepare(ActionWithMultipleDevices): class ToErased(ActionWithMultipleDevices): __doc__ = m.ToErased.__doc__ + document = NestedOn('Document', only_query='id') + # device = NestedOn(s_device.Device, only_query='id') class Live(ActionWithOneDevice): diff --git a/ereuse_devicehub/resources/action/views/documents.py b/ereuse_devicehub/resources/action/views/documents.py index 58d7b579..bb5c03ec 100644 --- a/ereuse_devicehub/resources/action/views/documents.py +++ b/ereuse_devicehub/resources/action/views/documents.py @@ -52,12 +52,15 @@ class ErasedView(): doc_data['type'] = 'ToErased' self.document = EraseDocument(**doc_data) db.session.add(self.document) - db.session.commit() + # db.session.commit() def insert_action(self, data): import pdb; pdb.set_trace() [data.pop(x, None) for x in ['url', 'documentId', 'filename', 'hash']] - self.data = self.schema.load(data) + # self.data = self.schema.load(data) + # self.data['document_id'] = self.document.id # self.data['document'] = self.document - self.erased = ToErased(**self.data) + # data['document_id'] = self.document.id + data['document'] = self.document + self.erased = ToErased(**data) db.session.add(self.erased) diff --git a/ereuse_devicehub/resources/documents/models.py b/ereuse_devicehub/resources/documents/models.py index 700cf59d..1dbee994 100644 --- a/ereuse_devicehub/resources/documents/models.py +++ b/ereuse_devicehub/resources/documents/models.py @@ -21,7 +21,7 @@ class Document(Thing): date = Column(db.DateTime, nullable=True) date.comment = """The date of document, some documents need to have one date """ - id_document = Column(CIText(), nullable=True) + id_document = Column(CIText(), nullable=False) id_document.comment = """The id of one document like invoice so they can be linked.""" owner_id = db.Column(UUID(as_uuid=True), db.ForeignKey(User.id),