Messages while migrating Ghost Blog from SQLite to MySQL - mysql

I tried to migrate ghost blog from SQLite to MySQL. I have completed the configuration as mentioned [here][1] I am getting following messages on command line. Is it correct or there is some issue in my config ?
$ npm start --production
> ghost#0.11.10 start /Ghost/Ghost
> node index
WARNING: Ghost is attempting to use a direct method to send email.
It is recommended that you explicitly configure an email service.
Help and documentation can be found at https://docs.ghost.org/v0.11.9/docs/mail-config.
[ { sql: 'show tables like ?',
output: [Function: output],
bindings: [ 'settings' ] } ]
{ method: 'first',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ 'databaseVersion', 1 ],
__knexQueryUid: '676a8f35-f680-4194-8214-0b9b54ba2kkk',
sql: 'select `value` from `settings` where `key` = ? limit ?' }
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [],
__knexQueryUid: 'a882bfa0-8b4d-4f04-a638-0c971ee9kkkk',
sql: 'select `settings`.* from `settings`' }
[ { sql: 'show tables like ?',
output: [Function: output],
bindings: [ 'settings' ] } ]
{ method: 'first',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ 'databaseVersion', 1 ],
__knexQueryUid: '984e224e-9909-434e-b1d2-0c971ee9kkkk',
sql: 'select `value` from `settings` where `key` = ? limit ?' }
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [],
__knexQueryUid: 'f6b8795b-7af2-4b39-a18d-0c971ee9kkkk',
sql: 'select `permissions`.* from `permissions`' }
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ 'activeTheme', 1 ],
__knexQueryUid: 'd5c924a7-4020-4cff-8a8d-0c971ee9kkkk',
sql: 'select `settings`.* from `settings` where `settings`.`key` = ? limit ?' }
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [],
__knexQueryUid: '6f7eb24c-e3c7-4197-a308-0c971ee9kkkk',
sql: 'select `settings`.* from `settings`' }
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ 'installedApps', 1 ],
__knexQueryUid: '0bf06724-21e3-46b0-9b9e-0c971ee9kkkk',
sql: 'select `settings`.* from `settings` where `settings`.`key` = ? limit ?' }
{ method: 'update',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings:
[ '2017-07-03 12:39:55',
1,
25,
'installedApps',
'app',
'2017-07-03 12:51:14',
1,
'f45a2c39-8be1-4858-a213-0c971ee9kkkk',
'[]',
25 ],
__knexQueryUid: '018b0cc4-f1c5-4e95-9dd0-0c971ee9kkkk',
sql: 'update `settings` set `created_at` = ?, `created_by` = ?, `id` = ?, `key` = ?, `type` = ?, `updated_at` = ?, `updated_by` = ?, `uuid` = ?, `value` = ? where `id` = ?' }
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ 1499086274940 ],
__knexQueryUid: 'aa394d2a-e425-4652-ab83-0c971ee9kkkk',
sql: 'select `accesstokens`.* from `accesstokens` where `expires` < ?' }
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ 1499086274941 ],
__knexQueryUid: '1a374b81-1c98-4d01-81d0-0c971ee9kkkk',
sql: 'select `refreshtokens`.* from `refreshtokens` where `expires` < ?' }
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ 'ghost-scheduler', 1 ],
__knexQueryUid: '0a851e78-5a7e-422a-9b5a-0c971ee9kkkk',
sql: 'select `slug`, `secret` from `clients` where `clients`.`slug` = ? limit ?' }
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [ 'scheduled' ],
__knexQueryUid: '0663f666-fe0f-4115-91ce-0c971ee9kkkk',
sql: 'select `id`, `published_at`, `created_at` from `posts` where `posts`.`status` = ?' }
Ghost is running in production...
Your blog is now available on http://localhost:2368
Ctrl+C to shut down
{ method: 'select',
options: {},
timeout: false,
cancelOnTimeout: false,
bindings: [],
__knexQueryUid: '287f9644-4baa-476a-84a7-0c971ee9kkkk',
sql: 'select `settings`.* from `settings`' }
[1]: http://www.techtrekking.net/how-to-migrate-ghost-blog-from-sqlite3-to-mysql/
Please note that blog us working fine but I am worried about these messages 1). if I have done any wrong config or 2). These extra display might impact server performance ?
Do I need to make any changes other than what is mentioned at http://www.techtrekking.net/how-to-migrate-ghost-blog-from-sqlite3-to-mysql/

Related

Find rows where association count is between passed values

I am trying to find all the rows with their count of associated model.
It should only return those rows which have count of associations between two passed values.
This is what I tried, but getting sql exception.
db.Employee.findAll({
where: {
$assignedJobCount$: {
[Op.between]: [0, 100],
},
},
include: [
{
model: db.AssignedJob,
required: true,
attributes: ['emp_id'],
},
],
attributes: [
'id',
'alternate_mobile',
['employee_unique_id', 'unique_id'],
[Sequelize.fn('COUNT', Sequelize.col('AssignedJobs.emp_id')), 'assignedJobCount'],
],
order: [['id', 'ASC']],
subQuery: false,
group: ['Employee.id'],
limit: 20,
});
return JSON.parse(JSON.stringify(mechanics));
};
It throws below error,
sql:
'SELECT
`Employee`.`id`,
`Employee`.`alternate_mobile`,
`Employee`.`employee_unique_id` AS `unique_id`,
COUNT(`AssignedJobs`.`emp_id`) AS `assignedJobCount`,
`AssignedJobs`.`id` AS `AssignedJobs.id`,
`AssignedJobs`.`emp_id` AS `AssignedJobs.emp_id`
FROM
`employee` AS `Employee`
INNER JOIN `assigned_jobs` AS `AssignedJobs` ON
`Employee`.`id` = `AssignedJobs`.`emp_id`
WHERE
`assignedJobCount` BETWEEN 0 AND 100
GROUP BY
`Employee`.`id`
ORDER BY
`Employee`.`id` ASC
LIMIT 20;',
parameters: undefined
}
SequelizeDatabaseError: Unknown column 'assignedJobCount' in 'where clause'
I fixed it by converting where clause into having clause as below.
db.Employee.findAll({
having: {
assignedJobCount: {
[Op.between]: [0, 100],
},
},
include: [
{
model: db.AssignedJob,
required: true,
attributes: ['emp_id'],
},
],
attributes: [
'id',
'alternate_mobile',
['employee_unique_id', 'unique_id'],
[Sequelize.fn('COUNT', Sequelize.col('AssignedJobs.emp_id')), 'assignedJobCount'],
],
order: [['id', 'ASC']],
subQuery: false,
group: ['Employee.id'],
limit: 20,
});
return JSON.parse(JSON.stringify(mechanics));
};

How to generate query with multiple or condtions in sequelize

I want to query with multiple or conditions in sequelize. Out of two, I have defined only one is reflected in the query generated using sequelize
Current Output:
SELECT `id`, `from_id`, `to_id`, `message`, `format`, `from_type`, `to_type`, `from_deleted_id`, `to_deleted_id`, `created_at`, `updated_at` FROM `messages` AS `Message` WHERE ((`Message`.`from_id` = '1' AND `Message`.`from_type` = 'user') OR
(`Message`.`from_id` = '3' AND `Message`.`from_type` = 'user'))
Expected Output:
SELECT `id`, `from_id`, `to_id`, `message`, `format`, `from_type`, `to_type`, `from_deleted_id`, `to_deleted_id`, `created_at`, `updated_at` FROM `messages` AS `Message` WHERE ((`Message`.`from_id` = '1' AND `Message`.`from_type` = 'user') OR
(`Message`.`from_id` = '3' AND `Message`.`from_type` = 'user')) AND ((`Message`.`to_id` = '1' AND `Message`.`to_type` = 'user') OR
(`Message`.`to_id` = '3' AND `Message`.`to_type` = 'user'))
where object -
where_condition={
[Op.or]: [
{from_id: from_id,from_type:from_type},
{from_id: to_id,from_type:to_type},
],
[Op.or]: [
{to_id: from_id,to_type:from_type},
{to_id: to_id,to_type:to_type},
],
}
Sequlize statement -
models.Message.findAll({where:where_condition})
Where object for having multiple or conditions must be as follow -
where_condition={
[Op.or]: [
{
[Op.and]: [
{
[Op.and]: [{ from_id: from_id }, { from_type: from_type }]
},
{
[Op.and]: [{ from_id: to_id }, { from_type: to_type }]
}
]
},
{
[Op.and]: [
{
[Op.and]: [{ to_id: from_id }, { to_type: from_type }]
},
{
[Op.and]: [{ to_id: to_id }, { to_type: to_type }]
}
]
}
]
}
Hope it helps!

Excluding foreign key attributes causes nested queries to fail

On all my models I have a created by and updated by foreign key columns that link to my user model. When I query my database using findAll() for example, I always include them so that the user's username and other details are returned with my results - I also exclude the foreign key columns for the result at it makes no sense returning the created by object which includes the ID and and the createdById node as it's just duplicate data
"createdById": "21499bb6-4476-11e6-beb8-9e71128cae77",
"createdBy": {
"id": "21499bb6-4476-11e6-beb8-9e71128cae77",
"username": "mo.gusbi"
},
"updatedById": "21499bb6-4476-11e6-beb8-9e71128cae77",
"updatedBy": {
"id": "21499bb6-4476-11e6-beb8-9e71128cae77",
"username": "mo.gusbi"
}
So what I do is just exclude createdById and updatedById attributes and this works great, until I have to join other tables and include constraints (for pagination, that cause a subquery to be generated) which looks for the createdById and updatedById columns that I originally excluded, causing an SQL error
SequelizeDatabaseError: SQLITE_ERROR: no such column: Category.createdById
from the generated query
SELECT `Category`.*, `children`.`id` AS `children.id`,
`children`.`name` AS `children.name`,
`createdBy`.`id` AS `createdBy.id`,
`createdBy`.`username` AS `createdBy.username`,
`updatedBy`.`id` AS `updatedBy.id`,
`updatedBy`.`username` AS `updatedBy.username`,
`deletedBy`.`id` AS `deletedBy.id`,
`deletedBy`.`username` AS `deletedBy.username`
FROM (
SELECT `Category`.`id`,
`Category`.`name`,
`Category`.`parentId`
FROM `Categories` AS `Category`
WHERE (`Category`.`deletedAt` IS NULL
AND `Category`.`parentId` = NULL)
LIMIT 0, 10
) AS `Category`
LEFT OUTER JOIN `Categories` AS `children`
ON `Category`.`id` = `children`.`parentId`
AND `children`.`deletedAt` IS NULL
LEFT OUTER JOIN `Users` AS `createdBy`
ON `Category`.`createdById` = `createdBy`.`id`
AND `createdBy`.`deletedAt` IS NULL
LEFT OUTER JOIN `Users` AS `updatedBy`
ON `Category`.`updatedById` = `updatedBy`.`id`
AND `updatedBy`.`deletedAt` IS NULL
LEFT OUTER JOIN `Users` AS `deletedBy`
ON `Category`.`deletedById` = `deletedBy`.`id`
AND `deletedBy`.`deletedAt` IS NULL
;
Models
User = sequelize.define('User', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV1,
primaryKey: true
},
username: {
type: DataTypes.STRING,
unique: true,
allowNull: false,
validate: {
notEmpty: true,
is: regex.username
}
}
}, {
paranoid: true,
classMethods: {
associate: (models) => {
User.belongsTo(models.User, {
foreignKey: 'createdById',
as: 'createdBy'
});
User.belongsTo(models.User, {
foreignKey: 'updatedById',
as: 'updatedBy'
});
User.belongsTo(models.User, {
foreignKey: 'deletedById',
as: 'deletedBy'
});
}
}
});
Category = sequelize.define('Category', {
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV1,
primaryKey: true
},
name: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notEmpty: true
}
}
}, {
indexes: [{
unique: true,
fields: ['name', 'parentId']
}],
paranoid: true,
classMethods: {
associate: (models: any) => {
Category.belongsTo(models.User, {
foreignKey: 'createdById',
as: 'createdBy'
});
Category.belongsTo(models.User, {
foreignKey: 'updatedById',
as: 'updatedBy'
});
Category.belongsTo(models.User, {
foreignKey: 'deletedById',
as: 'deletedBy'
});
Category.belongsTo(models.Category, {
foreignKey: 'parentId',
as: 'parent'
});
Category.hasMany(models.Category, {
foreignKey: 'parentId',
as: 'children'
});
}
}
});
Query
Category
.findAll({
attributes: [
'id',
'name',
'parentId'
],
where: {
parentId: req.query.parentId
},
include: [
{
model: Category,
as: 'children',
attributes: [
'id',
'name'
]
},
{
model: User,
as: 'createdBy',
attributes: [
'id',
'username'
]
},
{
model: User,
as: 'updatedBy',
attributes: [
'id',
'username'
]
},
{
model: User,
as: 'deletedBy',
attributes: [
'id',
'username'
]
}
],
offset: 0,
limit: 10
});

Sequelize generates a not working SQL when use order option

when I use order option in #findAll it generates SQL:
SELECT
`id`, `first_name` AS `firstName`,
`last_name` AS `lastName` FROM `customers` AS `Customer`
ORDER BY `Customer`.`firstName` DESC;
but this SQL causes error:
ER_BAD_FIELD_ERROR: Unknown column 'Customer.firstName' in 'order clause'
Code example:
var Customer = sequelize.define("Customer", {
id: {
type: Sequelize.INTEGER({unsigned: true}),
primaryKey: true
},
firstName: {
type: Sequelize.STRING(32),
field: "first_name"
},
lastName: {
type: Sequelize.STRING(32),
field: "last_name"
}
}, {
name: {
singular: "customer",
plural: "customers"
},
tableName: "customers",
timestamps: false,
underscored: true
});
Customer.findAll({
order: [["firstName", "DESC"]]
}).then(function(list) {
console.log(list);
}).catch(function(err) {
console.log(err);
});
Mysql: 5.6.20
Sequelize: 3.14.2
Are there any solutions of this issue?
Use order: [["first_name", "DESC"]]; the ORDER BY looks at column names, not your SELECT alias firstName.

Sequelize returns "Unknown column" when including tables

The idea is to get numbers that as associated with jobs on the call server. Each job has a bunch of numbers associated with it. When somebody calls back, I need to get the job associated with the caller ID.
The query that causes the issue:
// Look for messages to play
db.job.find( {
'where': { 'status' : [ 'new', 'inprogress', 'done' ] },
// 'limit' : 1, // Obviously
'order' : [[ 'jobID' , 'DESC' ]], // Latest
'include' : [
{
'model' : db.number,
'where' : { 'number' : this.number }
}
]
} ).complete( ... );
The database snippets:
var job = sequelize.define( 'job', {
'id': {
type: Sequelize.INTEGER( 10 ),
field: 'job_id',
allowNull: false,
primaryKey: true,
autoIncrement: true
},
'externalID' : {
type: Sequelize.STRING( 45 ),
field: 'external_id',
allowNull: false,
unique: true,
validate: {
notEmpty: true
}
},
'status' : {
type: Sequelize.STRING( 16 ),
field: 'status',
allowNull: false,
defaultValue: 'processing'
},
'filename' : {
type: Sequelize.STRING( 64 ),
field: 'filename',
allowNull: true
}
}, {
'createdAt' : 'created',
'updatedAt' : 'modified',
'tableName' : 'jobs'
} );
var number = sequelize.define( 'number', {
'id': {
type: Sequelize.BIGINT( 20 ),
field: 'number_id',
allowNull: false,
primaryKey: true,
autoIncrement: true
},
'jobID': {
type: Sequelize.INTEGER( 10 ),
field: 'job_id',
allowNull: false
},
'externalID' : {
type: Sequelize.STRING( 45 ),
field: 'external_id',
allowNull: false,
unique: true,
validate: {
notEmpty: true
}
},
'number' : {
type: Sequelize.STRING( 16 ),
field: 'number',
allowNull: false,
validate: {
notEmpty: true
}
},
'extension' : {
type: Sequelize.STRING( 16 ),
field: 'extension',
allowNull: true
},
'status' : {
type: Sequelize.STRING( 16 ),
field: 'status',
allowNull: false,
defaultValue: 'new'
},
'lastCall' : {
type: Sequelize.DATE,
field: 'last_call',
allowNull: true
},
'tries' : {
type: Sequelize.INTEGER,
field: 'tries',
allowNull: false,
defaultValue: 0
},
}, {
'createdAt' : 'created',
'updatedAt' : 'modified',
'tableName' : 'numbers'
} );
job.hasMany( number, { 'foreignKey' : 'job_id' } );
number.belongsTo( job, { 'foreignKey' : 'job_id' } );
Somehow the query it generates is this:
SELECT `job`.*,
`numbers`.`number_id` AS `numbers.id`,
`numbers`.`job_id` AS `numbers.jobID`,
`numbers`.`external_id` AS `numbers.externalID`,
`numbers`.`number` AS `numbers.number`,
`numbers`.`extension` AS `numbers.extension`,
`numbers`.`status` AS `numbers.status`,
`numbers`.`last_call` AS `numbers.lastCall`,
`numbers`.`tries` AS `numbers.tries`,
`numbers`.`created` AS `numbers.created`,
`numbers`.`modified` AS `numbers.modified`,
`numbers`.`job_id` AS `numbers.job_id`
FROM
(SELECT `job`.`job_id` AS `id`,
`job`.`external_id` AS `externalID`,
`job`.`status`,
`job`.`filename`,
`job`.`created`,
`job`.`modified`
FROM `jobs` AS `job`
WHERE `job`.`status` IN ('new',
'inprogress',
'done')
AND
(SELECT `job_id`
FROM `numbers` AS `numbers`
WHERE `job`.`id` = `numbers`.`job_id`
AND `numbers`.`number` IN ('5556656565') LIMIT 1) IS NOT NULL
ORDER BY `job`.`jobID` DESC LIMIT 1) AS `job`
INNER JOIN `numbers` AS `numbers` ON `job`.`id` = `numbers`.`job_id`
AND `numbers`.`number` IN ('5556656565')
ORDER BY `job`.`jobID` DESC;
Which as you can see by the table structure is giving this error:
error: Callback: Error fetching job via number.
SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'job.id' in
'where clause'
Where have I gone wrong here?
There seems to be an open issue about it, actually.
https://github.com/sequelize/sequelize/issues/3403