Postgresql 续集错误42804。将需要重写或转换表达式
我有一个与sequelize合作的项目,用于构建和同步数据库。我设置了一个名为Space的数据库表。当它尝试将数据库与我的其他表同步时。我得到一个错误代码42804 这是表格:Postgresql 续集错误42804。将需要重写或转换表达式,postgresql,sequelize.js,Postgresql,Sequelize.js,我有一个与sequelize合作的项目,用于构建和同步数据库。我设置了一个名为Space的数据库表。当它尝试将数据库与我的其他表同步时。我得到一个错误代码42804 这是表格: const seq = require('sequelize'); const { postgres } = require('../index'); const { Images } = require('./Images'); const { Reservation } = require('./Reservatio
const seq = require('sequelize');
const { postgres } = require('../index');
const { Images } = require('./Images');
const { Reservation } = require('./Reservation');
const { SpaceAmenities } = require('./SpaceAmenities');
const Space = postgres.define(
'space',
{
id: {type: seq.INTEGER, primaryKey: true, autoincrement: true},
size: {type: seq.INTEGER, require: true,
validate: {isNumeric: true, allowNull: false}
},
amount: {type: seq.FLOAT, require: true,
validate: {isFloat: true, allowNull: false}
},
floor: {type: seq.INTEGER, require: true,
validate: {isNumeric: true, allowNull: false}
},
available: {type: seq.BOOLEAN, require: true, defaultValue: 0,
validate: {isIn: [['0', '1']], isInt: true, allowNull: false}
}
},
{
createdAt: seq.DATE,
updatedAt: seq.DATE
}
)
Space.hasMany(Images, {as: 'imagesId'})
Space.hasMany(Reservation, {as: 'reservationId'})
Space.hasMany(SpaceAmenities, {as: 'spaceAmenitiesId'})
postgres.sync()
.then(() => {
console.log("Space table is connected and synced")
})
.catch((err) => {
console.log("Error syncing the Space table: " + JSON.stringify(err))
})
module.exports.Space = Space;
错误:
Error syncing the Space table: {"name":"SequelizeDatabaseError","parent":{"name":"error","length":184,"severity":"ERROR","code":"42804","hint":"You will need to rewrite or cast the expression.","file":"heap.c","line":"2946","routine":"cookDefault","sql":"CREATE TABLE IF NOT EXISTS \"spaces\" (\"id\" INTEGER , \"size\" INTEGER, \"amount\" FLOAT, \"floor\" INTEGER, \"available\" BOOLEAN DEFAULT 0, \"DATE\" TIMESTAMP WITH TIME ZONE NOT NULL, \"officeId\" INTEGER REFERENCES \"offices\" (\"id\") ON DELETE SET NULL ON UPDATE CASCADE, PRIMARY KEY (\"id\"));"},"original":{"name":"error","length":184,"severity":"ERROR","code":"42804","hint":"You will need to rewrite or cast the expression.","file":"heap.c","line":"2946","routine":"cookDefault","sql":"CREATE TABLE IF NOT EXISTS \"spaces\" (\"id\" INTEGER , \"size\" INTEGER, \"amount\" FLOAT, \"floor\" INTEGER, \"available\" BOOLEAN DEFAULT 0, \"DATE\" TIMESTAMP WITH TIME ZONE NOT NULL, \"officeId\" INTEGER REFERENCES \"offices\" (\"id\") ON DELETE SET NULL ON UPDATE CASCADE, PRIMARY KEY (\"id\"));"},"sql":"CREATE TABLE IF NOT EXISTS \"spaces\" (\"id\" INTEGER , \"size\" INTEGER, \"amount\" FLOAT, \"floor\" INTEGER, \"available\" BOOLEAN DEFAULT 0, \"DATE\" TIMESTAMP WITH TIME ZONE NOT NULL, \"officeId\" INTEGER REFERENCES \"offices\" (\"id\") ON DELETE SET NULL ON UPDATE CASCADE, PRIMARY KEY (\"id\"));"}
不确定如何通过同步修复此错误
还有其他与此表关联的表,包括
图像:
const seq = require('sequelize');
const { postgres } = require('../index');
const Images = postgres.define(
'images',
{
id: {type: seq.INTEGER, primaryKey: true, autoincrement: true},
image: {type: seq.FLOAT(9, 2), require: true,
validate: {isFloat: true, allowNull: false}
},
},
{
createdAt: seq.DATE,
updatedAt: seq.DATE
}
)
postgres.sync()
.then(() => {
console.log("Images table is connected and synced")
})
.catch((err) => {
console.log("Error syncing the Images table: " + JSON.stringify(err))
})
module.exports.Images = Images;
保留:
const seq = require('sequelize');
const { postgres } = require('../index');
const Reservation = postgres.define(
'reservation',
{
id: {type: seq.INTEGER, primaryKey: true, autoincrement: true},
start: {type: seq.DATE, required: true,
validate: {isDate: true, allowNull: false}
},
end: {type: seq.DATE, required: true,
validate: {isDate: true, allowNull: false}
},
amount: {type: seq.FLOAT(9, 2), require: true,
validate: {isFloat: true, allowNull: false}
},
},
{
createdAt: seq.DATE,
updatedAt: seq.DATE
}
)
postgres.sync()
.then(() => {
console.log("Reservation table is connected and synced")
})
.catch((err) => {
console.log("Error syncing the Reservation table: " + JSON.stringify(err))
})
module.exports.Reservation = Reservation;
空间设施:
const seq = require('sequelize');
const { postgres } = require('../index');
const SpaceAmenities = postgres.define(
'space_amenities',
{
id: {type: seq.INTEGER, primaryKey: true, autoincrement: true},
},
{
createdAt: seq.DATE,
updatedAt: seq.DATE
}
)
postgres.sync()
.then(() => {
console.log("Space_Amenities table is connected and synced")
})
.catch((err) => {
console.log("Error syncing the Space_Amenities table: " + JSON.stringify(err))
})
module.exports.SpaceAmenities = SpaceAmenities;
你的问题是
"available" BOOLEAN DEFAULT 0
PostgreSQL抱怨默认值无法隐式转换为布尔值:
\dCS boolean
List of casts
Source type | Target type | Function | Implicit?
-------------+-------------------+----------+---------------
boolean | character | text | in assignment
boolean | character varying | text | in assignment
boolean | integer | int4 | no
boolean | text | text | in assignment
integer | boolean | bool | no
jsonb | boolean | bool | no
(6 rows)
你应该写
"available" BOOLEAN DEFAULT FALSE