I need to create a constraint or trigger or foreign key for a table where I have columns 

ID, Name, Resource ID, Resource Type

Resource ID should be a foreign key to an existing table depending on what is selected in Resource Type.


So instead of having system_id and system_group_id I want it have just resource ID and the resource type so later it will be extendable 

class ABACPolicy(db.Model):
    id = db.Column(db.Integer(), primary_key=True)
    employee_id = db.Column(db.Integer(), db.ForeignKey("employee.id"), nullable=False)
    permission_id = db.Column(db.Integer(), db.ForeignKey("role.id"), nullable=False)
    system_group_id = db.Column(db.Integer(), db.ForeignKey("system_group.id"))
    system_id = db.Column(db.Integer(), db.ForeignKey("system.id"))

    __table_args__ = (
        db.UniqueConstraint("employee_id", "permission_id", "system_group_id", "system_id"),
        db.CheckConstraint("NOT(system_group_id IS NULL AND system_id IS NULL)", name="not_null_system_resource"),
    )

    employee = db.relationship("Employee", back_populates="abac_policy")
    role = db.relationship("Role", back_populates="abac_policy")
    system_group = db.relationship("SystemGroup", back_populates="abac_policy")
    system = db.relationship("System", back_populates="abac_policy")

Also, this should be for sqlalchemy python, which doesn't support check constraints with subqueries in it. It should be working with flask admin page as a drop-down list. Resource Type is a dropdown of System or System Group, Resource ID is a list of System or System Group names depending on the selection above. Maybe also add a column which says what action is allowed like "view", "edit", "create", "delete", etc. Write your response with exactly 3 paragraphs, separated with the markdown divider: ***.
user