balderdashy / balderdashy/sails
One to One associations
- Dominant language
- JavaScript
- Stars
- 22.8k
- Forks
- 1.9k
- PR merge metrics
- No merged PRs in 30d
Description
**Sails version**:1.1.0
**Node version**:v11.7.0
**NPM version**:6.8.0
**DB adapter name**: mysql
**DB adapter version**: 5.7
**Operating system**:mac
Hello,
I have an existing database for a website which I'm connecting sails to.
There are two tables I am trying to associate with a one to one relationship or a one way association. Both connect via a product_id field.
I've tried all different combinations from the documentation of the setttings provided. Each one giving a different error. Included some examples below.
Trimmed for convenience - options and errors.
Option 1
Error: no association returned
```
module.exports = {
tableName: 'oc_product',
attributes: {
createdAt: false,
updatedAt: false,
id: {
type: "number",
columnType: "int",
columnName: "product_id",
isInteger: true,
autoIncrement: true,
unique: true
},
oc_product_description: {
collection: 'ocProductDescription',
via: 'product_id'
}
}
};
module.exports = {
datastore: 'iclicksee',
tableName: 'oc_product_description',
attributes: {
createdAt: false,
updatedAt: false,
product_id: {
unique: true,
model: 'ocProducts',
},
}
}
```
Option 2
Error:
The model `ocproductdescription` defined a primary key of `product_id` that has an invalid type. Valid primary key types are `number` and `string`
Note: if I set the primary type it then complains that this should be picked up automatically by association
```
module.exports = {
tableName: 'oc_product',
primaryKey: 'product_id',
attributes: {
createdAt: false,
updatedAt: false,
product_id: {
type: "number",
columnType: "int",
isInteger: true,
autoIncrement: true,
unique: true
},
oc_product_description: {
model: 'ocProductDescription',
}
}
}
module.exports = {
datastore: 'iclicksee',
tableName: 'oc_product_description',
primaryKey: 'product_id',
attributes: {
createdAt: false,
updatedAt: false,
product_id: {
unique: true,
model: 'ocProducts',
},
}
}
```
Option 3
Error: Unexpected error from database adapter: ER_BAD_FIELD_ERROR: Unknown column 'oc_product.oc_product_description' in 'field list'
```
module.exports` = {
tableName: 'oc_product',
primaryKey: 'product_id',
attributes: {
createdAt: false,
updatedAt: false,
product_id: {
type: "number",
columnType: "int",
isInteger: true,
autoIncrement: true,
unique: true
},
oc_product_description: {
model: 'ocProductDescription',
}
}
}
module.exports = {
datastore: 'iclicksee',
tableName: 'oc_product_description',
primaryKey: 'product_id',
attributes: {
createdAt: false,
updatedAt: false,
product_id: {
type: "number",
unique: true,
},
}
}
```
Option 4:
Error: Unexpected error from database adapter: ER_BAD_FIELD_ERROR: Unknown column 'oc_product.oc_product_description' in 'field list'
```
module.exports = {
tableName: 'oc_product',
attributes: {
createdAt: false,
updatedAt: false,
id: {
type: "number",
columnType: "int",
columnName: "product_id",
isInteger: true,
autoIncrement: true,
unique: true
},
oc_product_description: {
model: 'ocProductDescription',
}
}
};
module.exports = {
datastore: 'iclicksee',
tableName: 'oc_product_description',
attributes: {
createdAt: false,
updatedAt: false,
id: {
columnName: 'product_id',
unique: true,
type: "number",
model: 'ocProducts',
},
}
}
```
Contributor guide
Research direction
The issue provides no repository file or test entry point; begin with the four model-definition examples and reproduce the reported association errors against the existing MySQL tables. Compare the generated field and primary-key behavior with the Sails association documentation. Done means a reproducible diagnosis or a focused failing test identifying the needed change.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mysql
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100