英文:
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模式。
示例:
关系:
用户模型:
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 :
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论