try solve problems with foreignkey

This commit is contained in:
Cayo Puigdefabregas 2021-07-21 15:02:23 +02:00
parent 2ec03d962e
commit c5e62914b6
5 changed files with 30 additions and 17 deletions

View File

@ -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()}')

View File

@ -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):

View File

@ -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):

View File

@ -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)

View File

@ -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),