Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 检查约束解决方案中禁止使用SQLAlchemy子查询_Python_Database_Sqlite_Sqlalchemy - Fatal编程技术网

Python 检查约束解决方案中禁止使用SQLAlchemy子查询

Python 检查约束解决方案中禁止使用SQLAlchemy子查询,python,database,sqlite,sqlalchemy,Python,Database,Sqlite,Sqlalchemy,我有一个使用声明式映射的数据库,其中有两个表,其中一个表需要至少一次检查另一个表的列中是否有特定值。我不能使用外键约束,因为该值不是唯一的 class Table_A(DeclarativeBase): classification_a = Column(String) classification_b = Column(String) value= Column(REAL) ... class Table_B(DeclarativeBase): nam

我有一个使用声明式映射的数据库,其中有两个表,其中一个表需要至少一次检查另一个表的列中是否有特定值。我不能使用外键约束,因为该值不是唯一的

class Table_A(DeclarativeBase):
    classification_a = Column(String)
    classification_b = Column(String)
    value= Column(REAL)
    ...

class Table_B(DeclarativeBase):
    name= Column(String)
    classification = Column(String)  # must be in Table_A.classification_a 
    value= Column(REAL)
    ...
示例表A

classification_a     classification_b     value
a                    a                    5 
a                    b                    8
a                    c                    0
b                    a                    1
b                    b                    6
b                    c                    8
c                    a                    9
c                    b                    7
c                    c                    9
我想要什么:

当我在
表中插入B
时,分类
等于a、B或c->在数据库中插入新行

分类的所有其他值都应引发错误

我的尝试:

我尝试在
表B
中使用CheckConstraint,它将完全实现我想要的功能,如以下代码:

CheckConstraint("classification  IN (SELECT classification_a FROM Table_A)")
这会导致
sqlalchemy.exc.OperationalError:(sqlite3.OperationalError)检查约束中禁止子查询
,因为sqlite3不支持检查中的子查询


在这个问题上,有人建议使用
@validates
装饰器,但我不知道该如何检查此方法中的其他表列,因为我无权使用装饰器访问该方法中的会话。

问题:

如何在
表B
类中实现我的“约束”

软件:

Python 3.6.4

SQLAlchemy 1.2.7


sqlite版本:3.15.2

如何从
表a
中删除记录?数据可以删除吗?@Danila Ganchar为什么要从表A中删除数据?我不知道是否可以从DeclarativeBase类中的不同表中删除数据。在表B中插入后,我需要表A中与表B中插入前相同的内容。从
表A
中删除记录如何?数据可以删除吗?@Danila Ganchar为什么要从表A中删除数据?我不知道是否可以从DeclarativeBase类中的不同表中删除数据。在表_B中插入之后,我需要表_A中与表_B中插入之前相同的内容。