KSDaemon/sails-hook-sequelize

Sync is causing an error when I start sails for a second time

tomascharad opened this issue · 9 comments

Describe the bug
When I start the server for a second time I get:
SequelizeDatabaseError: relation "email_unique_idx" already exists

To Reproduce
Just start the app two times

Expected behavior
To have a clean start without errors

Screenshots
image

Desktop (please complete the following information):

  • OS: Mac Os High Sierra
  • Node.js version 10.1.0
  • Sequelize version 4.38.0

Additional context
Seems to be related with: sequelize/sequelize#7606
But I'n not sure if for any reason sync is being called twice as by some config of sails v1 (sequelize/sequelize#7606 (comment))

Hi @tomascharad! Well, no, i don't see any reason or flow, when sails call sync twice. As i can see from log, yes, the only reason is that table Feedbacks with column email and even constraint email_unique_idx already exists. So alter table add constraint... with same constraint name fails.

For a quick workaround you can start sails once with migrate: alter option, and then change to migrate: safe

Hi @KSDaemon

I think that the SQL statements are being triggered multiple times.
Do you have any clue why this could be happening?
image

It seems that for every connection defined, the hook creates a connection

image

So if I have:

default, production and development datastores, the hook creates three different connections.

@tomascharad Thanks for information! I have some thoughts how to fix it...

Perfect @KSDaemon, would love to help if you need some.

@tomascharad I've just got some time to investigate this...
So, you have 3 datastores, which all point to same db?
If you want to separate your environments, you can config sails accordingly, using config/env/development.js or config/env/production.js.

I think @Damienmarble pathc fixes this issue.