Update
I have now go back to a previous build of doctrine and now the error is:
Invalid schema element named "Roles" at path "RoleResource->columns->relations"
this is whit the same yaml file (see it below)
I have a problem with the doctrine command line tool. When I give the command "build-all-reload", I get te following error:
SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table. Failing Query: "CREATE TABLE resource (id BIGINT AUTO_INCREMENT, name VARCHAR(20), INDEX role_id_idx (role_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB". Failing Query: CREATE TABLE resource (id BIGINT AUTO_INCREMENT, name VARCHAR(20), INDEX role_id_idx (role_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE = INNODB
My yaml file looks like this:
detect_relations: true
options:
type: INNODB
collate: utf8_general_ci
charset: utf8
Log:
columns:
id:
type: integer
primary: true
autoincrement: true
priority: tinyint
priorityName: string(10)
title: string(250)
message: text
actAs:
Timestampable:
created:
type: timestamp
format: Y-m-d H:i:s
updated:
disabled: true
User:
columns:
id:
type: integer
primary: true
autoincrement: true
username: string(50)
password: string(40)
actAs:
Timestampable:
created:
type: timestamp
format: Y-m-d H:i:s
updated:
type: timestamp
format: Y-m-d H:i:s
Role:
columns:
id:
type: integer
primary: true
autoincrement: true
name: string(20)
attributes:
export: all
validate: true
RoleResource:
columns:
role_id:
type: integer
primary: true
resource_id:
type: integer
primary: true
relations:
Role:
foreignAlias: RoleResource
Resource:
foreignAlias: RoleResource
Resource:
columns:
id:
type: integer
primary: true
autoincrement: true
name: string(20)
relations:
Roles:
foreignAlias: Resources
class: Role
refClass: RoleResource
Menu:
columns:
id:
type: integer
primary: true
autoincrement: true
label: string(20)
Artical:
columns:
id:
type: integer
primary: true
autoincrement: true
title: string
content: longtext
css: longtext
js: longtext
I don't know how to solve this problem.
Can someone please help me?
I downgraded Doctrine.
And there are to spaces to much in the yaml file add the line:
relations:
from the component "RoleResource:"
Thank you for all the help Tom,
Ivo Trompert
Related
i am writing in typescript,
i am trying this in member model:
mail: {
type: String,
unique: true,
nullable: true,
default: null,
}
Is it possible to reference composite primary keys in Sequelize?
I'm working on a web-app that helps organize kitchen waste. The restaurant organizes its weeks and months into 'periods' where the first week of September would be '9.1'. For every period, I need to create a new batch of ingredient objects that can keep track of what their prices and quantities were for that period. I figure it would be best to make the period primary keys their combined month and week, as that will be unique in the database.
I may add year on later, but that doesn't change my problem.
The database I'm working with is Postgres.
This is my period table model in my sequelize seed file:
.then(() => queryInterface.createTable('periods', {
month: {
type: Sequelize.INTEGER,
validate: {
max: 12,
min: 1
},
unique: "monthWeekConstraint",
primaryKey: true
},
week: {
type: Sequelize.INTEGER,
validate: {
max: 4,
min: 1
},
unique: "monthWeekConstraint",
primaryKey: true
},
createdAt: {
type: Sequelize.DATE
},
updtedAt: {
type: Sequelize.DATE
}
}))
I'd like to reference the periods stored in the above table in my periodItems table, which I have (incorrectly) looking like:
.then(() => queryInterface.createTable('periodItems', {
periodMonth: {
type: Sequelize.INTEGER,
references: {model: 'periods', key: 'monthWeekConstraint'}
},
periodWeek: {
type: Sequelize.INTEGER,
references: {model: 'periods', key: 'monthWeekConstraint'}
},
day: {
type: Sequelize.INTEGER,
validate: {
min: 1,
max: 7
}
},
...other irrelevant fields...
}))
I'm definitely new to databases, so I apologize if I'm way off. I've gotten a few other tables doing what I'd like, but I've been stuck on this problem for a few days.
While it is possible to create composite primary keys in Sequelize by specifying primaryKey: true against more than one column (as you have already done above), Sequelize doesn't currently support composite foreign keys, so there is no way to reference a model/table which has composite primary keys.
See https://github.com/sequelize/sequelize/issues/311 for a discussion on the subject.
model/product.js:
const Product = sequelize.define("product", {
sku: { type: Sequelize.STRING, allowNull: false, primaryKey: true },
title: { type: Sequelize.STRING, allowNull: false },
availability: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: false,
}
});
model/Attribute.js:
const Attribute = sequelize.define("attribute", {
key: { type: Sequelize.STRING, allowNull: false, primaryKey: true },
productSku: { type: Sequelize.STRING, allowNull: false, primaryKey: true },
value: { type: Sequelize.STRING, allowNull: false },
});
After importing to app.js:
product.hasMany(attribute, { foreignKey: "productSku", sourceKey: "sku" });
attribute.belongsTo(product, { foreignKey: "productSku", targetKey: "sku" });
Explanation:
Product.sku is exported as foreign key to Attibute.productSku. Attribute table has a composite foreign (key + productSku), and a ForeignKey(productSku) from product.sku;
I am working with Sequelize and I'm trying to insert one row into my User Column. However, I keep getting the following error:
Unhandled rejection SequelizeDatabaseError: Invalid object name 'User'.
I am connecting to a MSSQL server. I know that I have the basic connection right because I can run sequelize's plain queries via sequelize.query without issue. My suspicion is that I'm not specifying the schema or database correctly.
My model definition:
var user = sequelize.define("User", {
ID: {
type: Sequelize.INTEGER,
unique: true,
autoIncrement: true
},
FirstName: {
type: Sequelize.STRING
},
LastName: {
type: Sequelize.STRING
},
CreateDate: {
type: Sequelize.DATE
},
UpdateDate: {
type: Sequelize.DATE
}
},{
tableName: 'User',
timestamps: false,
freezeTableName: true
});
My attempt to use the model to create/INSERT a row into my pre-existing database.
User.schema('Core');
User.create({ ID: '1', FirstName: 'Bobert', LastName: 'Jones'}).then(function(user) {
console.log(user.get({
plain: true
}))
});
When I used Sequelize's plain SQL to accomplish this I DID have to include the schema aka "insert into Core.User". Am I not specifying the schema correctly? I have tried adding it to my initial connection definition by adding "schema: 'Core'," before dialectOptions.
you can specify schema in your sequelize constructor:
var sequelize = new Sequelize("db",
"user",
"pass", {
host: "localhost",
port: "1206",
dialect: "mssql",
define: {
schema: "core"
}
});
However, according to their code what you are doing appears correct. In the sequelize constructor you can also turn logging on (logging: true). Logging should output the exact sql that is being constructed.
How can I associate two tables in Sequelize? I tried belongsTo, but this doesn't work. Example:
First table:
users = sequelize.define('users', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
name: Sequelize.TEXT,
type: Sequelize.INTEGER
Second table:
profiles = sequelize.define('profiles', {
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
},
place: Sequelize.TEXT,
phone: Sequelize.INTEGER
Association:
profiles.belongsTo(users, {foreignKey: 'id'});
Request:
users.findOne({
where: {name: 'John'},
include: [{model: db.tables.profiles}]
}).then(function(user_data) {
console.log(user_data);
})
Returned [Error: profiles is not associated to users!]
I need to return the matched line of "users" and the line with the same id from the table 'profiles'. Where is the mistake?
You need to declare the association for both tables. From your schema I can't tell what the join condition is. If your profiles table also has a column user_id such that profiles.user_id = users.id, then you could say the following:
users.hasMany(profiles, {
foreignKey: 'id'
});
profiles.belongsTo(users, {
foreignKey: 'user_id'
});
I have below model with primary key id:
attributes: {
id: {
type: 'integer',
autoIncrement: true,
primaryKey: true,
unique: true
},
name: {
type: 'string',
unique: true,
required: true
},
}
I am creating model as below:
var model = {
id: undefined,
name: 'name',
};
waterlinemodel.create(model).exec(function(error, result) {});
But it throws below error:
Error (E_UNKNOWN) Encountered an unexpected error] Details: error: null value in column "id" violates not-null constraint
As, 'id' is a primary key, waterline should not look at what is the value of 'id' property.
How to resolve this error? I do not want to remove 'id' because I have created value object for the model and it contains all the attributes of model.I am setting value object property as I need. I do not need to set id property for creation.
I am having exactly the same problem especially with the model configured to use postgresql. With it set to disk or memory, the resource is created but with postgresql the resource is not created with the not null constraint error.
The id is not being set irrespective of whether I set autoPK: true or not. Even setting the id attribute on the model with autoPK:false doesn't work.
As the documentation says :
Will set the primary key of the record. This should be used when autoPK is set to false.
attributes: {
uuid: {
type: 'string',
primaryKey: true,
required: true
}
}
You need to set autoPK : false on your model.
The link to the doc : https://github.com/balderdashy/waterline-docs/blob/master/models.md#primarykey
2019 Answer
jaumard's link to documentation is coming up with a 404 error now, but I think things may have changed since 2015...
Sails.js has base attributes defined in config/models.js which looks something this in a freshly-generated project:
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'number', autoIncrement: true, },
}
Separately, the default primaryKey is set to id. If you wanted to override that, you would need to explicitly specify your new primaryKey in your full model definition. For example, if you wanted to make name your primaryKey you would use something like this:
module.exports = {
primaryKey: 'name',
attributes: {
name: {
type: 'string',
unique: true,
required: true
},
// ...
},
}
Notice I put primaryKey outside attributes. This is important. Your primaryKey will also need the unique and required constraints.
Further, if you want to disable the id column so it is not committed to your database, you must replace id with the value false -- but then you must define a different primaryKey otherwise you will get an error when you start your application. The error shown in the question may be directly related to the fact that the model defined id explicitly as undefined. An example of how to disable id would look something like this:
module.exports = {
primaryKey: 'name',
attributes: {
id: false,
name: {
type: 'string',
unique: true,
required: true
},
// ...
},
}