English 中文(简体)
D. 在集延戈建立JOIN数据库
原标题:Doing database JOIN in django

I want to do equivalent query as SELECT user_name, item_name FROM users, items WHERE users.favor_item_id = items.item_id, which is to return user_name and item_name pairs. In the database, one user can have multiple favoured items.

我只想问一下一下一下一下一下一下一下一下一下一下,KQQQ是什么?

我的第一项想法是列出所有(用户,赞成项目_id)的电梯,然后看项目_名称和id项目。 但看来,它将在使用上述Kingk查询时,复杂性是O(NlogM)(M是ITEM的多个项目),而复杂的是O(N)。

是否在贾戈(或任何办公室管理系统)有更有效的方法这样做?

问题回答

(或是否有任何管理系统)?

真的? 页: 1

假设合理的地图级(如使用申报):

Base = sqlalchemy.ext.declarative.declarative_base()

class Item(Base):
    __tablename__ =  items 
    id = Column( item_id , Integer, primary_key=True)
    name = Column( item_name , String)

class User(Base):
    __tablename__ =  users 
    name = Column( user_name , String(50), primary_key=True)
    favor_item_id = Column(Integer, ForeignKey(Item.id))

    favor_item = relationship(Item, backref="favored_by")

问题是简单的

>>> print sqlalchemy.orm.Query((User.name, Item.name)).join(Item.favored_by)
SELECT users.user_name AS users_user_name, items.item_name AS items_item_name 
FROM items JOIN users ON items.item_id = users.favor_item_id

假设模型:

class User(models.Model):
    name = models.CharField(max_length=32)
    favorite = models.ForeignKey(Item)

class Item(models.Model):
    name = models.CharField(max_length=32)

相应的Django是:

usersFavorites = User.objects.all().values_list("name", "favorite__name")

这并不使用你提供的田地的确切名称。 为此,您仅需要将这些模型添加适当的<代码>db_table和db_column领域。

P.S. 这一特别图谋并不特别好。 IMHO,User and Item should have a many-to-many relationship:

class User(models.Model):
    name = models.CharField(max_length=32)
    favorites = models.ManyToManyField(Item, related_name="users")

class Item(models.Model):
    name = models.CharField(max_length=32)

除非项目只有一个用户,在这种情况下是<代码>。 外国Key 项目

之后,你可以翻一番:

for user in User.objects.all().select_related("favorites"):
    for favorite in user.favorites:
        # use `user` and `favorite` in some way.

Because of the select_related() call, the query is done in one fell swoop in the first for loop.





相关问题
what is wrong with this mysql code

$db_user="root"; $db_host="localhost"; $db_password="root"; $db_name = "fayer"; $conn = mysqli_connect($db_host,$db_user,$db_password,$db_name) or die ("couldn t connect to server"); // perform query ...

Users asking for denormalized database

I am in the early stages of developing a database-driven system and the largest part of the system revolves around an inheritance type of relationship. There is a parent entity with about 10 columns ...

Easiest way to deal with sample data in Java web apps?

I m writing a Java web app in my free time to learn more about development. I m using the Stripes framework and eventually intend to use hibernate and MySQL For the moment, whilst creating the pages ...

join across databases with nhibernate

I am trying to join two tables that reside in two different databases. Every time, I try to join I get the following error: An association from the table xxx refers to an unmapped class. If the ...

How can I know if such value exists in database? (ADO.NET)

For example, I have a table, and there is a column named Tags . I want to know if value programming exists in this column. How can I do this in ADO.NET? I did this: OleDbCommand cmd = new ...

Convert date to string upon saving a doctrine record

I m trying to migrate one of my PHP projects to Doctrine. I ve never used it before so there are a few things I don t understand. In my current code, I have a class similar to this: class ...

热门标签