简体   繁体   English

对cqlengine(Cassandra CQL 3 Object Mapper for Python)的数据模型设计存在疑问

[英]Data model design doubts on cqlengine(Cassandra CQL 3 Object Mapper for Python)

I am using cqlengine , I had few doubts: 我正在使用cqlengine ,我对此几乎没有疑问:

If we have a model defined as 如果我们有一个模型定义为

class Contact(Model):
   street = columns.Text()
   city = columns.Text()

Can we do something like this 我们可以做这样的事情吗

  1. Reference to contact model like mentioned in code below 引用下面的代码中提到的联系模型

    class User(Model):

      contact = Contact() first_name =coloumns.Text() .....` 
  2. And Reference to User model as list: 并参考用户模型列表:

    class Company(Model): users = columns.List(User()) ..... Company(Model)类: users = columns.List(User()) .....

Are above correct way to achive the needed functionality, Or any other way ? 以上是实现所需功能的正确方法还是其他方法?

Not really. 并不是的。 Cassandra has no concept of foreign key relations, so neither does cqlengine. Cassandra没有外键关系的概念,因此cqlengine也没有。 When you're modeling relationships like this with cassandra, there are a few approaches you can use, depending on the behavior of the related. 当您使用cassandra对这种关系进行建模时,有几种方法可以使用,具体取决于相关对象的行为。

The first would be to duplicate your data. 首先是复制您的数据。 In the case of tying a user to an address, it might make sense to save the full address into the user table. 在将用户绑定到地址的情况下,将完整地址保存到用户表中可能是有意义的。 With things that don't change (123 A Street won't magically move to 456 B street), this makes sense. 事情不会改变(123 A街不会神奇地移动到456 B街),这是有道理的。 If the user moves locations, you can just update the user table. 如果用户移动位置,则可以只更新用户表。

The second is to have a unique id for each contact location, and save the relevant ids on the user model, usually in a set. 第二种是为每个联系人位置使用唯一的ID,并将相关的ID通常存储在用户模型中。 This is often better when 2 related entities change independently of each other, and the overhead of going out and updating the duplicated data each time something is changed is too high for your application. 当两个相关实体彼此独立更改时,这通常会更好,并且每次更改某些内容时,外出和更新重复数据的开销对于您的应用程序来说太高了。

The third is a variation on the second method, where the relating ids are stored in another table, which acts as a join table (user_id -> contact_id). 第三个是第二种方法的变体,其中相关的id存储在另一个表中,该表充当联接表(user_id-> contact_id)。

In any case, the most important thing to remember when working with cassandra/cqlengine, is that you want to model your data to answer a query. 在任何情况下,使用cassandra / cqlengine时要记住的最重要的事情是要对数据进行建模以回答查询。 Duplicating your data is ok. 复制数据是可以的。 Don't try to model your data like you would in an RDBMS. 不要像在RDBMS中那样尝试对数据建模。

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

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