部署在Heroku的应用提示PostgreSQL列不存在,但本地运行正常
将应用部署在Heroku上,生产与开发环境均使用PostgreSQL,通过knex进行数据库查询。页面无法加载资源,执行heroku log --tail时收到错误:提示books.author_id列不存在,但该查询在本地环境可正常运行,且通过pgadmin确认数据库中该列确实存在。
相关代码
数据库查询代码:
function getBookById(id) { console.log(id) return db('books') .join('authors', 'books.author_id', 'authors.id') .where('books.id', id) .select('*', 'authors.name as author_name', 'authors.id as author_id') .first() }
错误日志详情
进一步排查发现多个数据库请求失败并返回503状态码,错误日志如下:
2022-08-24T02:25:36.434109+00:00 app[web.1]: at Parser.parseErrorMessage (/app/node_modules/pg-protocol/dist/parser.js:287:98) 2022-08-24T02:25:36.434109+00:00 app[web.1]: at Parser.handlePacket (/app/node_modules/pg-protocol/dist/parser.js:126:29) 2022-08-24T02:25:36.434110+00:00 app[web.1]: at Parser.parse (/app/node_modules/pg-protocol/dist/parser.js:39:38) 2022-08-24T02:25:36.434110+00:00 app[web.1]: at TLSSocket.<anonymous> (/app/node_modules/pg-protocol/dist/index.js:11:42) 2022-08-24T02:25:36.434111+00:00 app[web.1]: at TLSSocket.emit (node:events:513:28) 2022-08-24T02:25:36.434112+00:00 app[web.1]: at addChunk (node:internal/streams/readable:315:12) 2022-08-24T02:25:36.434112+00:00 app[web.1]: at readableAddChunk (node:internal/streams/readable:289:9) 2022-08-24T02:25:36.434112+00:00 app[web.1]: at TLSSocket.Readable.push (node:internal/streams/readable:228:10) 2022-08-24T02:25:36.434113+00:00 app[web.1]: at TLSWrap.onStreamRead (node:internal/stream_base_commons:190:23) { 2022-08-24T02:25:36.434113+00:00 app[web.1]: length: 127, 2022-08-24T02:25:36.434114+00:00 app[web.1]: severity: 'ERROR', 2022-08-24T02:25:36.434114+00:00 app[web.1]: code: '42703', 2022-08-24T02:25:36.434114+00:00 app[web.1]: detail: undefined, 2022-08-24T02:25:36.434115+00:00 app[web.1]: hint: undefined, 2022-08-24T02:25:36.434115+00:00 app[web.1]: position: '22', 2022-08-24T02:25:36.434115+00:00 app[web.1]: internalPosition: undefined, 2022-08-24T02:25:36.434115+00:00 app[web.1]: internalQuery: undefined, 2022-08-24T02:25:36.434116+00:00 app[web.1]: where: undefined, 2022-08-24T02:25:36.434116+00:00 app[web.1]: schema: undefined, 2022-08-24T02:25:36.434116+00:00 app[web.1]: table: undefined, 2022-08-24T02:25:36.434116+00:00 app[web.1]: column: undefined, 2022-08-24T02:25:36.434117+00:00 app[web.1]: dataType: undefined, 2022-08-24T02:25:36.434117+00:00 app[web.1]: constraint: undefined, 2022-08-24T02:25:36.434117+00:00 app[web.1]: file: 'parse_target.c', 2022-08-24T02:25:36.434118+00:00 app[web.1]: line: '1061', 2022-08-24T02:25:36.434118+00:00 app[web.1]: routine: 'checkInsertTargets' 2022-08-24T02:25:36.434118+00:00 app[web.1]: } 2022-08-24T02:25:36.434240+00:00 app[web.1]: There is an error here: insert into "books" ("author_id", "blurb", "cover_image", "genre", "pub_year", "title") values ($1, $2, $3, $4, $5, $6) returning "id" - column "author_id" of relation "books" does not exist
排查与解决方案
确认Heroku数据库结构:通过Heroku CLI连接生产数据库,直接查询表结构,验证
books表是否存在author_id列。执行命令:heroku pg:psql # 进入数据库后执行 \d books该命令会列出
books表的所有字段,对比本地表结构是否一致。检查迁移脚本执行状态:如果使用knex迁移,确认生产环境的迁移是否全部执行完成。执行命令:
heroku run knex migrate:status若存在未执行的迁移,运行:
heroku run knex migrate:latest验证数据库连接配置:确认应用在Heroku上使用的数据库URL是否正确,是否连接到预期的PostgreSQL实例。执行命令查看配置:
heroku config:get DATABASE_URL对比本地数据库配置,排除连接错误数据库的可能。
检查标识符大小写:PostgreSQL对表名、列名等标识符的大小写敏感,如果创建表时使用带引号的列名(如
"Author_Id"),查询时必须保持一致。检查迁移脚本中books表的author_id列定义是否存在大小写不一致的情况。重启应用:若迁移执行完成后问题仍存在,尝试重启Heroku应用:
heroku restart
内容的提问来源于stack exchange,提问作者vygrdev
相关产品推荐
相关产品推荐

