You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+7-1Lines changed: 7 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -14,7 +14,13 @@ Sequelize is a promise-based Node.js/io.js ORM for Postgres, MySQL, MariaDB, SQL
14
14
15
15
From 3.0.0 and up Sequelize will follow SEMVER.
16
16
17
-
[Upgrading to 2.0](https://github.com/sequelize/sequelize/wiki/Upgrading-to-2.0)
17
+
## Backwards compatibility changes
18
+
19
+
3.0.0 cleans up a lot of deprecated code, making it easier for us to develop and maintain features in the future. This implies that most of the changes in 3.0.0 are breaking changes! Please read the changelog for 3.0.0 carefully.
20
+
[Upgrading to 3.0](https://github.com/sequelize/sequelize/wiki/Upgrading-to-3.0).
21
+
We highly recommend to use 3.0 as it also includes security related fixes that can't be backported to either 2.0 or 1.7.
22
+
23
+
If you still use 1.7 please read our guide [Upgrading to 2.0](https://github.com/sequelize/sequelize/wiki/Upgrading-to-2.0) plus the changelog up to now. Version 2.1 also contained new breaking changes.
Copy file name to clipboardExpand all lines: changelog.md
+8-6Lines changed: 8 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,29 +2,31 @@
2
2
3
3
3.0.0 cleans up a lot of deprecated code, making it easier for us to develop and maintain features in the future.
4
4
5
-
-[ADDED] findById/findByPrimary takes a single value as argument representing the primary key to find.
5
+
-[ADDED] findById / findByPrimary takes a single value as argument representing the primary key to find.
6
6
-[CHANGED] belongsToMany relations MUST now be given a `through` argument.
7
-
-[CHANGED] findOne/findAll/findAndCount/findOrCreate now only takes a single options argument instead of a options, queryOptions argument. So set transaction, raw, etc on the first options argument.
7
+
-[CHANGED] findOne / findAll / findAndCount / findOrCreate now only takes a single options argument instead of a options and queryOptions argument. So set transaction, raw, etc on the first options argument.
8
8
-[CHANGED] The accessor for belongsToMany relationships is now either the `as` argument or the target model name pluralized.
9
9
-[REMOVED] N:M relationships can no longer be represented by 2 x hasMany
10
-
-[REMOVED] Model.create/Model.bulkCreate no longer takes an array of fields as its second argument, use `options.fields` instead.
11
-
-[REMOVED] Query Chainer has been remved
10
+
-[REMOVED] Model.create / Model.bulkCreate / Instance.save no longer takes an array of fields as its second argument, use `options.fields` instead.
11
+
-[REMOVED] Query Chainer has been removed
12
12
-[REMOVED] Migrations have been removed, use umzug instead
13
13
-[REMOVED] Model.findAllJoin has been removed
14
-
-[REMOVED] sequelize.query now only takes `sql, callee, options` as arguments, the 4th argument `replacements`has been removed and should be set via `options.replacements` instead.
14
+
-[REMOVED] sequelize.query now only takes `sql and options` as arguments, the second and fourth argument `callee` and `replacements`have been removed and should be set via`options.instance` / `options.model` and `options.replacements` instead.
15
15
-[REMOVED]`instance.isDirty` has been removed, use `instance.changed()` instead
16
16
-[REMOVED]`instance.values` has been removed, use `instance.get()` instead
17
17
-[REMOVED]`instance.primaryKeyValues` has been removed.
18
+
-[REMOVED]`instance.identifiers` has been removed, use `instance.where()` instead
18
19
-[REMOVED]`instance.isDeleted` has been removed, simply check the timestamp with `get('deletedAt')` instead
19
20
-[REMOVED]`instance.increment/decrement` now longer takes a number as it's second argument.
20
-
-[REMOVED/SECURITY] findOne no longer takes a string/integer/binary argument to represent a primaryKey. Use findById instead
21
+
-[REMOVED/SECURITY] findOne no longer takes a string / integer / binary argument to represent a primaryKey. Use findById instead
21
22
-[REMOVED/SECURITY]`where: "raw query"` is no longer legal, you must now explicitely use `where: ["raw query", [replacements]]`
22
23
-[BUG] Fix showIndexQuery so appropriate indexes are returned when a schema is used
23
24
-[BUG] Fix addIndexQuery error when the model has a schema
24
25
-[BUG] Fix app crash in sqlite while running in special unique constraint errors [3730](https://github.com/sequelize/sequelize/pull/3730)
25
26
-[BUG] Fix bulkCreate: do not insert NULL for undefined values [3729](https://github.com/sequelize/sequelize/pull/3729)
26
27
-[BUG] Fix trying to roll back a comitted transaction if an error occured while comitting resulting in an unhandled rejection [3726](https://github.com/sequelize/sequelize/pull/3726)
27
28
-[BUG] Fix regression in beforeUpdate hook where `instance.changed()` would always be false [3727](https://github.com/sequelize/sequelize/pull/3727)
29
+
-[BUG] Fix trying to roll back a comitted transaction if an error occured while comitting
28
30
29
31
#### Backwards compatibility changes
30
32
- Most of the changes in 3.0.0 are BC breaking, read the changelog for 3.0.0 carefully.
@@ -144,13 +144,13 @@ Defining `through` is required. Sequelize would previously attempt to autogenera
144
144
This will add methods `getUsers`, `setUsers`, `addUsers` to `Project`, and `getProjects`, `setProjects` and `addProject` to `User`.
145
145
146
146
Sometimes you may want to rename your models when using them in associations. Let's define users as workers and projects as tasks by using the alias (`as`) option:
To add a new project to a user and set it's status, you pass an extra object to the setter, which contains the attributes for the join table
173
-
173
+
174
174
```js
175
175
user.addProject(project, { status:'started' })
176
176
```
177
177
178
178
By default the code above will add ProjectId and UserId to the UserProjects table, and _remove any previously defined primary key attribute_ - the table will be uniquely identified by the combination of the keys of the two tables, and there is no reason to have other PK columns. To enforce a primary key on the `UserProjects` model you can add it manually.
You can also check if an object is already associated with another one (N:M only). Here is how you'd do it:
482
-
482
+
483
483
```js
484
484
// check if an object is one of associated ones:
485
485
Project.create({ /**/ }).then(function(project) {
@@ -520,64 +520,64 @@ Task.belongsTo(User)
520
520
```
521
521
522
522
Will generate the following SQL:
523
-
523
+
524
524
```sql
525
525
CREATETABLEIF NOT EXISTS `User` (
526
-
`id`INTEGERPRIMARY KEY,
526
+
`id`INTEGERPRIMARY KEY,
527
527
`username`VARCHAR(255)
528
528
);
529
-
529
+
530
530
CREATETABLEIF NOT EXISTS `Task` (
531
-
`id`INTEGERPRIMARY KEY,
532
-
`title`VARCHAR(255),
531
+
`id`INTEGERPRIMARY KEY,
532
+
`title`VARCHAR(255),
533
533
`user_id`INTEGERREFERENCES`User` (`id`) ON DELETESETNULLONUPDATE CASCADE
534
534
);
535
535
```
536
536
537
-
The relation between task and user injects the `user_id` foreign key on tasks, and marks it as a reference to the `User` table. By default `user_id` will be set to `NULL` if the referenced user is deleted, and updated if the id of the user id updated. These options can be overridden by passing `onUpdate` and `onDelete` options to the association calls. The validation options are `RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL`.
537
+
The relation between task and user injects the `user_id` foreign key on tasks, and marks it as a reference to the `User` table. By default `user_id` will be set to `NULL` if the referenced user is deleted, and updated if the id of the user id updated. These options can be overridden by passing `onUpdate` and `onDelete` options to the association calls. The validation options are `RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL`.
538
538
539
539
For 1:1 and 1:m associations the default option is `SET NULL` for deletion, and `CASCADE` for updates. For n:m, the default for both is `CASCADE`. This means, that if you delete or update a row from one side of an n:m association, all the rows in the join table referencing that row will also be deleted or updated.
540
540
541
541
Adding constraints between tables means that tables must be created in the database in a certain order, when using `sequelize.sync`. If Task has a reference to User, the User table must be created before the Task table can be created. This can sometimes lead to circular references, where sequelize cannot find an order in which to sync. Imagine a scenario of documents and versions. A document can have multiple versions, and for convenience, a document has an reference to it's current version.
542
-
542
+
543
543
```js
544
544
varDocument=this.sequelize.define('Document', {
545
545
author:Sequelize.STRING
546
546
})
547
547
, Version =this.sequelize.define('Version', {
548
548
timestamp:Sequelize.DATE
549
549
})
550
-
550
+
551
551
Document.hasMany(Version) // This adds document_id to version
552
552
Document.belongsTo(Version, { as:'Current', foreignKey:'current_version_id'}) // This adds current_version_id to document
553
553
```
554
554
555
555
However, the code above will result in the following error: `Cyclic dependency found. 'Document' is dependent of itself. Dependency Chain: Document -> Version => Document`. In order to alleviate that, we can pass `constraints: false` to one of the associations:
`document_id`INTEGERREFERENCES`Document` (`id`) ON DELETESETNULLONUPDATE CASCADE
574
574
);
575
575
```
576
576
577
577
### Enforcing a foreign key reference without constraints
578
578
579
579
Some times you may want to reference another table, without adding any constraints, or associations. In that case you can manually add the reference attributes to your schema definition, and mark the relations between them.
0 commit comments