sql_app: db: Fixed column copy warning.

A warning was raised whenever the user<->device
	relation was accessed. The assumption is that
	now the queries perform faster, having fixed
	this issue.

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-06-05 17:10:35 +03:00
parent 232ff82c46
commit 244b7c6b91
Signed by: Hesham
GPG Key ID: 74876157D199B09E
2 changed files with 11 additions and 6 deletions

View File

@ -70,9 +70,11 @@ def create_iot_entity(db: Session, iot_entity: schemas.IotEntityCreate):
def create_user_link_to_iot(db: Session, user_id: int, iot_dev_id: int): def create_user_link_to_iot(db: Session, user_id: int, iot_dev_id: int):
# Ensure link is not already present and it does not allow duplicates # Ensure link is not already present and it does not allow duplicates
link = db.query(models.UserAuthToIoTDev).filter(models.UserAuthToIoTDev.user_id == user_id).filter(models.UserAuthToIoTDev.iot_entity_id == iot_dev_id).first() link = db.query(models.UserAuthToIoTDev).filter(models.UserAuthToIoTDev.user_id == user_id).filter(models.UserAuthToIoTDev.iot_id == iot_dev_id).first()
if link: return True if link: return True
new_link = models.UserAuthToIoTDev(user_id=user_id, iot_entity_id=iot_dev_id) new_link = models.UserAuthToIoTDev(user_id=user_id,
iot_id=iot_dev_id,
timestamp=datetime.now())
db.add(new_link) db.add(new_link)
db.commit() db.commit()
db.refresh(new_link) db.refresh(new_link)

View File

@ -15,7 +15,8 @@ class User(Base):
is_active = Column(Boolean, default=True, nullable=False) is_active = Column(Boolean, default=True, nullable=False)
last_token = Column(String, nullable=True) last_token = Column(String, nullable=True)
authorized_devices = relationship("IotEntity", secondary= 'user_iot_link') authorized_devices = relationship("IotEntity", secondary="user_iot_link", back_populates="authorized_users")
#authorized_devices = relationship("IotEntity", secondary= 'user_iot_link')
class IotEntity(Base): class IotEntity(Base):
@ -26,13 +27,15 @@ class IotEntity(Base):
description = Column(String(512)) description = Column(String(512))
open_request = Column(Boolean, default=False) open_request = Column(Boolean, default=False)
time_seconds = Column(Integer, default=10) time_seconds = Column(Integer, default=10)
authorized_users = relationship("User", secondary= 'user_iot_link') authorized_users = relationship("User", secondary="user_iot_link", back_populates="authorized_devices")
#authorized_users = relationship("User", secondary= 'user_iot_link')
class UserAuthToIoTDev(Base): class UserAuthToIoTDev(Base):
__tablename__ = "user_iot_link" __tablename__ = "user_iot_link"
user_id = Column(Integer, ForeignKey('user_accounts.id'), primary_key=True, index=True) user_id = Column(Integer, ForeignKey("user_accounts.id"), primary_key=True)
iot_entity_id = Column(Integer, ForeignKey('iot_entities.id'), primary_key=True, index=True) iot_id = Column(Integer, ForeignKey("iot_entities.id"), primary_key=True)
timestamp = Column(DateTime)
class DoorAccessLog(Base): class DoorAccessLog(Base):
__tablename__ = "door_access_log" __tablename__ = "door_access_log"