简体   繁体   English

SQLAlchemy / MySQL的表设计

[英]Table design for SQLAlchemy/MySQL

I've created a database in MySQL that has the following tables 我已经在MySQL中创建了一个具有以下表格的数据库

Attachment
Sender
Recipient
Message
mr_link
ms_link
ma_link

Sender and Recipient are distinct tables of email addresses mr_link contains the ids of a message and its associated recipients (one entry per recipient per message) 发件人和收件人是电子邮件地址的不同表。mr_link包含邮件的ID及其关联的收件人(每封邮件每个收件人一个条目)

ms_link has the ids of a message and its associated sender ms_link具有消息ID及其关联的发件人

ma_link has the ids of a message and its associated file(s) ma_link具有消息的ID及其关联的文件

I've recently discovered SQLAlchemy and like how it will make my queries, etc. more efficient. 我最近发现了SQLAlchemy,并且喜欢它如何使我的查询等更加高效。

What I'm looking to do now is convert this existing schema into tables with relationships, but my MySQL-fu and SQLAlchemy-fu are still in their early stages. 我现在想要做的就是将这个现有的模式转换成具有关系的表,但是我的MySQL-fu和SQLAlchemy-fu仍处于早期阶段。

I've come here for guidance. 我来这里是为了指导。

Thanks, Larry 谢谢,拉里

You have one major choice in implementing your model in SA, which is between using the ORM and going with straight SQL constructs. 在SA中实现模型时,您有一个主要选择,即使用ORM和使用直接SQL构造之间。 This can be confusing at first, because they have similar methods and behaviors. 起初这可能会造成混淆,因为它们具有相似的方法和行为。 At the outset as you dig in, it might be useful to think of them in terms of the classes you work with: 从一开始,您就可以根据所使用的类来考虑它们:

  • using the SQL i/f, you will work primarily with Tables 使用SQL i / f,您将主要使用表
  • using the ORM, you will work primarily with your classes (Messages, Recipients, Senders,etc) 使用ORM,您将主要处理您的类(消息,收件人,发件人等)

In either case, you are likely to find yourself first implementing your primary entities and then following through to set up the many-to-many relationships between them. 无论哪种情况,您都可能首先发现自己实现了主要实体,然后逐步建立它们之间的多对多关系。 SA does some great stuff, but it is pretty deep. SA做得很好,但是内容很深。 I recommend giving yourself some time to work through the pieces. 我建议给自己一些时间来完成各个部分。

To that end, I would recommend spending some time in the Object Relational Tutorial . 为此,我建议您花一些时间在“对象关系教程”中 The BlogPost/Keyword example in the Many-to-Many section has some useful characteristics in common with your use case and should give you a place to start from. 在博客帖子/关键字例子多到很多部分具有一些共同的有用的特征与您的使用情况下,应该给你从一个地方开始。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM