So, I'm trying to learn full stack by making a list app, and I'm using Sequelize.js for the database. So far I've got User and List models. I recently switched to Sequelize v7.0.0-alpha.47. I can't quite remember, but I believe it worked for a bit using v7; however, now I'm getting this strange error:
Error: SQLITE_ERROR: near "RETURNING": syntax error
at new BaseError (/home/projects/listless-qzftb29s/node_modules/@sequelize/core/src/errors/base-error.ts:35:5)
at new DatabaseError (/home/projects/listless-qzftb29s/node_modules/@sequelize/core/src/errors/database-error.ts:32:5)
at SqliteQuery.formatError (/home/projects/listless-qzftb29s/node_modules/@sequelize/sqlite3/src/query.js:303:16)
at executeSql (/home/projects/listless-qzftb29s/node_modules/@sequelize/sqlite3/src/query.js:197:20)
at eval (/home/projects/listless-qzftb29s/node_modules/@sequelize/core/src/sequelize.js:380:16)
Here is the database main code. I believe the error is occurring from saving the model:
import { sequelize } from "./models";
import User from "./models/user.model";
await sequelize.sync();
const maxCodes = (
await User.findOrCreate({ // <<< error here
where: { username: "maxCodes" },
})
)[0];
I also tried it like this:
import { sequelize } from "./models";
import User from "./models/user.model";
await sequelize.sync();
const [maxCodes, built] = (
await User.findOrBuild({
where: { username: "maxCodes" },
})
);
if (built) maxCodes.save(); // async error w/out await, so it must occur at saving
I'm using @sequelize/[email protected] and
@sequelize/[email protected].
Here's my user model:
import { Model, DataTypes } from "@sequelize/core";
import type {
HasManyAddAssociationMixin,
HasManyCountAssociationsMixin,
HasManyCreateAssociationMixin,
HasManyGetAssociationsMixin,
HasManyHasAssociationMixin,
HasManySetAssociationsMixin,
HasManyAddAssociationsMixin,
HasManyHasAssociationsMixin,
HasManyRemoveAssociationMixin,
HasManyRemoveAssociationsMixin,
NonAttribute,
CreationOptional,
InferAttributes,
InferCreationAttributes,
} from "@sequelize/core";
import {
Attribute,
PrimaryKey,
AutoIncrement,
NotNull,
HasMany,
} from "@sequelize/core/decorators-legacy";
import List from "./list.model";
class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
@Attribute(DataTypes.INTEGER)
@PrimaryKey
@AutoIncrement
id!: CreationOptional<number>;
@Attribute(DataTypes.STRING)
@NotNull
username!: string;
@HasMany(() => List, {
foreignKey: "userId",
inverse: {
as: "user",
},
})
lists?: NonAttribute<List[]>;
declare getLists: HasManyGetAssociationsMixin<List>; // Note the null assertions!
declare addList: HasManyAddAssociationMixin<List, number>;
declare addLists: HasManyAddAssociationsMixin<List, number>;
declare setLists: HasManySetAssociationsMixin<List, number>;
declare removeList: HasManyRemoveAssociationMixin<List, number>;
declare removeLists: HasManyRemoveAssociationsMixin<List, number>;
declare hasList: HasManyHasAssociationMixin<List, number>;
declare hasLists: HasManyHasAssociationsMixin<List, number>;
declare countLists: HasManyCountAssociationsMixin<List>;
declare createList: HasManyCreateAssociationMixin<List, "userId">;
declare createdAt: CreationOptional<Date>;
declare updatedAt: CreationOptional<Date>;
}
export default User;
Here is my List model code:
import { Model, DataTypes } from "@sequelize/core";
import type {
NonAttribute,
ForeignKey,
CreationOptional,
InferAttributes,
InferCreationAttributes,
BelongsToGetAssociationMixin,
BelongsToCreateAssociationMixin,
BelongsToSetAssociationMixin,
} from "@sequelize/core";
import {
Attribute,
PrimaryKey,
AutoIncrement,
NotNull,
BelongsTo,
} from "@sequelize/core/decorators-legacy";
import User from "./user.model";
class List extends Model<InferAttributes<List>, InferCreationAttributes<List>> {
@Attribute(DataTypes.INTEGER)
@PrimaryKey
@AutoIncrement
id!: CreationOptional<number>;
@Attribute(DataTypes.STRING)
@NotNull
name!: string;
@BelongsTo(() => User, {
foreignKey: "userId",
inverse: {
as: "lists",
type: "hasMany",
},
})
user!: NonAttribute<User>;
@Attribute(DataTypes.INTEGER)
@NotNull
userId!: ForeignKey<User["id"]>;
// timestamps!
// createdAt can be undefined during creation
declare createdAt: CreationOptional<Date>;
// updatedAt can be undefined during creation
declare updatedAt: CreationOptional<Date>;
declare getUser: BelongsToGetAssociationMixin<User>;
declare createUser: BelongsToCreateAssociationMixin<User>;
declare setUser: BelongsToSetAssociationMixin<User, User["id"]>;
}
export default List;
And finally, my Sequelize config:
import { Sequelize } from "@sequelize/core";
import { SqliteDialect } from "@sequelize/sqlite3";
import User from "./user.model";
import List from "./list.model";
const sequelize = new Sequelize({
dialect: SqliteDialect,
storage: "data/database.sqlite",
models: [User, List],
});
export { sequelize, Sequelize };
I honestly have no idea what is happening. Is this an error on my part, or an error in Sequelize itself? Why am I even getting this error? (Like what code is causing the error?)
If you want to see the full code (note: much more than just server-side), you can check out the live demo. If you think that the error is coming from elsewhere in the code, instead of what I've shown here, please comment and I'll review my question and code.
