为什么我们必须在模型和迁移中设置数据库的关系?Adonis JS

huangapple go评论73阅读模式
英文:

Why we have to set the relations of the database at model and migration? Adonis JS

问题

为什么需要在 Adonis.js 中设置数据库迁移中存在的关系(带有引用),以及在模型中设置hasMany关系?

英文:

Why is needed at Adonis.js set the relationship that exists at a database on Migrations (with reference) and at Models (with hasMany)?

答案1

得分: 0

总结一下,迁移(migrations)和模型(models)是独立的,它们不必一起使用。要使用模型,不需要使用迁移(您可以使用现有的数据库)。

迁移(Migrations)

迁移用于创建数据库。

您必须在迁移文件中定义数据库的结构,因此关系非常重要。

官方文档中:

>
迁移是记录在应用程序开发生命周期中创建的数据库变更,您可以随时回滚或重新运行这些变更。

>
迁移使团队合作更容易,可以轻松跟踪来自一名开发人员的数据库模式更改,并由组织中的其他开发人员应用。

当然,并不一定要使用迁移。您可以创建自己的数据库,只使用模型来检索数据。

模型 - 关系3

关于模型(Lucid)的有趣之处在于可以轻松进行SQL查询(使用Knex)。

由于模型和迁移没有直接关联,因此需要声明要使用的表格之间的不同关系。

Lucid是活动记录模式的AdonisJS实现。

官方文档中:

>
关系是数据驱动应用程序的基础,将一种模型类型链接到另一种模型类型。
>
例如,用户可以具有多个帖子关系,每个帖子可以具有多个评论关系。
>
Lucid的表达性API使关联和获取模型关系的过程变得简单直观,无需触摸SQL语句甚至编辑SQL模式。

示例:

关系:

为什么我们必须在模型和迁移中设置数据库的关系?Adonis JS

用户模型:

const Model = use('Model')

class User extends Model {
  posts () {
    return this.hasMany('App/Models/Post') // 具有多个帖子
  }
}

module.exports = User

控制器代码:

const User = use('App/Models/User')

const users = await User
  .query()
  .with('posts') // 使用模型关系
  .fetch()

输出:

[
  {
    id: 1,
    username: 'virk',
    posts: [{
      id: 1,
      user_id: 1,
      post_title: '...',
      post_body: '...',
    }]
  }
]

^ 来源

英文:

In summary, migrations and models are independent and do not work together. To use the models it's not necessary to use migrations (you can use an existing database).

Migrations

Migrations are used to create the db.

You have to define the structure of the db in the migration files. So relationships are important.

From official documentation :
>
Migrations are documented database mutations, created throughout your application’s development lifecycle that you can roll back or re-run at any point in time.

>
Migrations make it easier to work as a team, enabling database schema changes from one developer to be easily tracked and then applied by other developers in your organization.

Of course, it's not necessary to use migration. You can create your own db and use only the models that allow you to retrieve the data.

Models - Relationships

The interesting thing about models (Lucid) is that it's easy to make SQL queries (with Knex).

Since models and migrations are not linked, it's necessary to declare the different relationships between the tables you are going to use.

Lucid is the AdonisJS implementation of the active record pattern.

From official documentation:

>
Relationships are the backbone of data-driven applications, linking one model type to another.
>
For example, a User could have many Post relations, and each Post could have many Comment relations.
>
Lucid’s expressive API makes the process of associating and fetching model relations simple and intuitive, without the need to touch a SQL statement or even edit a SQL schema.

Example :

Relations :

为什么我们必须在模型和迁移中设置数据库的关系?Adonis JS

User model :

const Model = use('Model')

class User extends Model {
  posts () {
    return this.hasMany('App/Models/Post') // has many Post
  }
}

module.exports = User

Controller code :

const User = use('App/Models/User')

const users = await User
  .query()
  .with('posts') // Use model relationship
  .fetch()

Output :

[
  {
    id: 1,
    username: 'virk',
    posts: [{
      id: 1,
      user_id: 1,
      post_title: '...',
      post_body: '...',
    }]
  }
]

^ Source

huangapple
  • 本文由 发表于 2020年1月3日 13:59:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/59573845.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定