question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

relation does not exist for multiple hasOne/belongsTo

See original GitHub issue
#############
# Model
#############

Node = sequelize.define('node', {
  id: {
    type:       Sequelize.INTEGER
    primaryKey: true
  }
}, {
  timestamps: false
})

Beacon = sequelize.define('beacon', {
  major: Sequelize.INTEGER
  minor: Sequelize.INTEGER
}, {
  timestamps: false
})

StaticDevice = sequelize.define('staticDevice' ,{
  coordinates:  Sequelize.GEOMETRY
  type:         Sequelize.ENUM('NODE', 'BEACON', 'BOTH')
  battery:      Sequelize.FLOAT
})

#############
# Relations
#############

StaticDevice.hasOne(Node)
StaticDevice.hasOne(Beacon)

#############
# sync
#############
StaticDevice.sync(force: true)
Node.sync(force: true)
Beacon.sync(force: true)

I get this error:

relation "staticDevices" does not exist

Why does this exists? im really lost, im doing exactly what the docs say.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
benjaminhoncommented, Jun 6, 2016

My bad, I guess i should be just using sequelize.sync(). Because i was doing as per the docs, copying the examples, but just syncing them manually.

However, please correct me if i am wrong, but i am just copying this directly from the docs http://docs.sequelizejs.com/en/latest/docs/associations/#creating-elements-of-a-belongsto-or-hasone-association

var Product = this.sequelize.define('product', {
  title: Sequelize.STRING
});
var User = this.sequelize.define('user', {
  first_name: Sequelize.STRING,
  last_name: Sequelize.STRING
});

Product.belongsTo(User);

return Product.create({
  title: 'Chair',
  User: {
    first_name: 'Mick',
    last_name: 'Broadstone'
  }
}, {
  include: [ User ]
});

However, in the table, only products get create, no entry for users is created.

2reactions
janmeiercommented, Jun 6, 2016

Node and Beacon have foreign key references to StaticDevice. Each call to sync is asyncronous, so when you fire them of like they were serial, the StaticDevice is not created at the time you try to create the two other tables. You can use promises (.then), es7 (await), or simply sequelize.sync() to have sequelize handle syncing the tables in the correct order

Closing this issue, since it is neither a bug report nor a feature request.

For general sequelize questions, please use Slack, StackOverflow or Google groups.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I solve "Relation 'a' is not instance of HasOne or ...
If the query executed, there exist error like this : Relation 'a' is not instance of HasOne or BelongsTo. How can I solve...
Read more >
Eloquent: Relationships - The PHP Framework For Web Artisans
An example of a many-to-many relationship is a user that has many roles and those roles are also shared by other users in...
Read more >
hasOne without belongsTo (i.e. one-way relationships)
I have two tables, let's call them property and address , plus some other tables. Every property has a foreign key which references...
Read more >
hasOne Relation | LoopBack Documentation
A hasOne relation denotes a one-to-one connection of a model to another model through referential integrity. The referential integrity is enforced by a...
Read more >
Difference between has one belongs to and has many
The only difference between hasOne and belongsTo is where the foreign key column is located. Let's say you have two entities: User and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found