New schema cheatsheet
This cheatsheet summarises the changes needed to update your database to use the new Keystone database schema, introduced in the Arcade
release.
For full instructions please consult the migration guide.
One-to-many (one-sided)
JS
keystone.createList('User', { fields: { name: { type: Text } } });
keystone.createList('Post', {
fields: {
title: { type: Text },
content: { type: Text },
author: { type: Relationship, ref: 'User', many: false },
},
});
Migration strategy
- No changes are required for these relationships.
Many-to-many (one-sided)
JS
keystone.createList('User', { fields: { name: { type: Text } } });
keystone.createList('Post', {
fields: {
title: { type: Text },
content: { type: Text },
authors: { type: Relationship, ref: 'User', many: true },
},
});
Migration Strategy
PostgreSQL
- Rename
Post_authors
toPost_authors_many
. - Rename
Post_id
toPost_left_id
andUser_id
toUser_right_id
.
MongoDB
- Create a collection
post_authors_manies
with fieldsPost_left_id
andUser_right_id
. - Move the data from
posts.authors
intopost_authors_manies
. - Delete
posts.authors
.
One-to-many (two-sided)
JS
keystone.createList('User', {
fields: {
name: { type: Text },
posts: { type: Relationship, ref: 'Post.author', many: true },
},
});
keystone.createList('Post', {
fields: {
title: { type: Text },
content: { type: Text },
author: { type: Relationship, ref: 'User.posts', many: false },
},
});
Migration strategy
PostgreSQL
- Drop the
User_posts
table.
MongoDB
- Remove
users.posts
.
Many-to-many (two-sided)
JS
keystone.createList('User', {
fields: {
name: { type: Text },
posts: { type: Relationship, ref: 'Post.authors', many: true },
},
});
keystone.createList('Post', {
fields: {
title: { type: Text },
content: { type: Text },
authors: { type: Relationship, ref: 'User.posts', many: true },
},
});
Migration strategy
PostgreSQL
- Drop the
Post_authors
table. - Rename
User_posts
toUser_posts_Post_authors
. - Rename
User_id
toUser_left_id
andPost_id
toPost_right_id
.
MongoDB
- Create a collection
user_posts_post_authors
with fieldsUser_left_id
andPost_right_id
. - Move the data from
users.posts
intouser_posts_post_authors
. - Delete
users.posts
. - Delete
posts.authors
.
One-to-one (two-sided)
JS
keystone.createList('User', {
fields: {
name: { type: Text },
post: { type: Relationship, ref: 'Post.author', many: false },
},
});
keystone.createList('Post', {
fields: {
title: { type: Text },
content: { type: Text },
author: { type: Relationship, ref: 'User.post', many: false },
},
});
Migration strategy
PostgreSQL
One to one relationships in the before
state had a foreign key column on each table.
In the after
state, only one of these is stored.
Because of the symmetry of the one-to-one relationship, Keystone makes an arbitrary decision about which column to use.
- Identify the foreign key column which is no longer required, and delete it.
- In our example above we would delete the
Post.author
column.
MongoDB
One to one relationships in the before
state had a field in each collection.
In the after
state, only one of these is stored.
Because of the symmetry of the one-to-one relationship, Keystone makes an arbitrary decision about which field to use.
- Identify the field which is no longer required, and delete it.
- In our example above we would delete the
post.author
field.