gpt4 book ai didi

python - 在谷歌应用引擎中设计数据库

转载 作者:搜寻专家 更新时间:2023-10-30 22:02:39 25 4
gpt4 key购买 nike

我刚接触GAE,所以如果你想帮忙,请写一些细节和例子。

我正在尝试做两个数据库模型,用户和文章。每个用户可以有一些文章。在 sql server 中它将是:

create table User
(
id int primary key identity(1,1),
login nvarchar(50) unique not null,
password nvarchar(50) not null,
email nvarchar(50) unique not null,
)

create table Article
(
userId int references User(id) not null,
topic nvarchar(50) not null,
content nvarchar(max) not null
)

在 python 中我尝试:

class Article(db.Model):
topic = db.StringProperty(multiline=False)
content = db.StringProperty(multiline=True)

class User(db.Model):
login = db.StringProperty()
email = db.EmailProperty()
password = db.StringProperty(multiline=False)
articles = db.ListProperty(int) #here I want to do db.ListProperty(Article), but I can't. So I want to keep here id of article.

我的问题是:

  • 我怎样才能保证登录名和电子邮件是唯一的
  • 如何获取用户的主键(在 sql 中

     select id from User where login = 'sada' and password = 'sd'
  • 如何使用这个主键来搜索用户
  • 如果我想在用户文章中保留 id,如何为用户添加新文章

也许这是更好的方法,很高兴我会知道更好的解决方案

最佳答案

首先,Google AppEngine Datastore 不是关系数据库。这是一个完全不同的范例。也许你应该先看看 Datastore Overview documentationMastering the datastore .

关于您的具体问题:

  1. 唯一登录名和电子邮件:您必须检查它是否已经存在,因为数据存储不提供唯一约束。您还可以查看此解决方案:Add a Unique Constraint to Google App Engine .或者你可以使用 Google Accounts .
  2. Primary key for UserPrimary key to search user:使用 Google AppEngine 可以直接获取用户:user = db.GqlQuery("SELECT *来自登录为 :1"的用户,登录)
  3. 引用:这是一篇关于它的非常好的文章:Modeling entity relationships

关于python - 在谷歌应用引擎中设计数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8955954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com