[英]Is there a Non-Sqlalchemy way to deal with many-to-many relationships in Python?
I've searched for quite a long time on the web for a method that deals with many-to-many relationships in python sqlite3, but all seems to lead to Sqlalchemy. 我已经在网络上搜索了很长一段时间的方法,该方法可以处理python sqlite3中的多对多关系,但是所有这些似乎都导致了Sqlalchemy。 I'm not against using sqlalchemy at all(although I do find it an overkill from time to time and it does introduce some unnecessary logic in many cases), I was wondering if there is a 'golden class/function' that provides basic CRUD interface directly without bothering Sqlalchemy?
我一点也不反对使用sqlalchemy(尽管我确实不时发现它过于矫kill过正,并且在许多情况下确实引入了一些不必要的逻辑),我想知道是否存在提供基本CRUD的“黄金类/函数”界面直接而不打扰Sqlalchemy? Any references (online or paper-based) will be highly appreciated.
任何参考(在线或纸质)将受到高度赞赏。
If you want to solve many-to-many relations in basic SQL you can do this manualy using third table for storing those relations. 如果要在基本SQL中解决多对多关系,则可以使用第三张表来手动存储存储这些关系。
CREATE TABLE users {
int user_id,
varchar user_name
};
CREATE TABLE categories {
int category_id,
varchar category_name
};
CREATE TABLE category_permission {
int user_id,
int category_id
}; -- for storing relations
These three tables represents two models ( user
, category
) and one many-to-many relation ( category_permission
) 这三个表代表两个模型(
user
, category
)和一个多对多关系( category_permission
)
You have to query them manualy and also manualy maintain stored relations. 您必须手动查询它们,也必须手动维护存储的关系。 Based on SQL engine you are using you should consider using
基于您正在使用的SQL引擎,您应该考虑使用
You can then select this way: 然后可以选择这种方式:
-- to list all users and their category count
SELECT U.user_name, count(CP.category_id) as 'permitted'
FROM users U
LEFT JOIN category_permission PM
ON PM.user_id = U.user_id
ORDER BY permitted DESC;
-- to list all categories for __desired_user__
SELECT C.* FROM categories C
JOIN category_permission CM
ON CM.category_id = C.category_id
WHERE CM.user_id = __desired_user_id__;
For further reference search for SQL solution instead of Python (which will always lead you to some framework). 有关更多参考,请搜索SQL解决方案而不是Python(这将始终导致您使用某些框架)。 Many-to-many relationship is a common problem in relation databases.
多对多关系是关系数据库中的常见问题。
If you are looking at other ORMs than SqlAlchemy, you can compare the ORMs presented here: What are some good Python ORM solutions? 如果您正在查看SqlAlchemy以外的其他ORM,则可以比较此处介绍的ORM : 什么是一些好的Python ORM解决方案?
I personnaly tried Storm that supports Many-to-Many almost transparently but you have to write SQL to create the table (IIRC). 我曾亲自尝试过Storm ,它几乎透明地支持多对多,但您必须编写SQL才能创建表(IIRC)。
I also tried autumn which is dead but a fork exists: AutORM . 我还尝试了已经死了但有叉子的秋天: AutORM 。 It is very lightweight but doesn't support many-to-many.
它非常轻巧,但不支持多对多。 You probably can work that around by declaring your junction table explicitly and it is targeted to sqlite (see peewee).
您可能可以通过显式声明联结表来解决该问题,该联结表以sqlite为目标(请参阅peewee)。
I tested dejavu (dead) and peewee which also doesn't have many-to-many transparently but explains how to do it in its docs (you can apply the same to AutORM). 我测试了dejavu(死)和peewee ,它们也没有多对多的透明对象,但是在其文档中说明了如何进行 (您可以将其应用于AutORM)。
For my own case I used SqlAlchemy finally because the machinery necessary for ORMs is anyway so big that I preferred getting more functions for the price and at that time Python 3 support was not that common (my two cents :-) ). 对于我自己的情况,我最终使用了SqlAlchemy,因为ORM所需的机制太大了,以至于我更愿意以价格购买更多的功能,而那时对Python 3的支持并不那么普遍(我的两分钱:-))。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.