You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Android端TypeORM与CodePush更新问题求助

React Native + TypeORM + CodePush 问题解决指南

问题一:Entity metadata for t#user was not found

原因分析

本地构建正常但CodePush更新报错,核心原因是CodePush打包过程中TypeORM的装饰器元数据丢失或实体引用解析失败:

  • 使用@ManyToOne('User')字符串参数时,CodePush的混淆/压缩可能破坏了实体名称的映射关系
  • 路径别名~/*在CodePush构建时未正确解析,导致实体未被DataSource正确注册

解决方案

  1. 统一实体引用方式并确保元数据保留
    避免单一写法的兼容性问题,改用兼容方案:

    // 在User实体类上显式指定表名
    @Entity({ name: 'user' })
    export class User { /* ... */ }
    
    // 在关联字段使用表名字符串引用,同时确保实体被导入(用于元数据生成)
    import { User } from './user';
    @ManyToOne('user') // 这里用@Entity指定的name属性值
    @JoinColumn({name: 'user_pk'})
    user!: User;
    

    这样既规避了iOS的循环依赖,又能让TypeORM正确识别实体元数据。

  2. 验证CodePush的路径别名解析
    确保babel.config.js中的module-resolver在CodePush构建时生效,可临时在实体文件中打印require.resolve('~/database/entities/user'),对比本地和CodePush环境的解析结果是否一致。

问题二:migration name is wrong. Migration class name should have a JavaScript timestamp appended.

原因分析

TypeORM迁移类命名规则要求类名末尾必须是13位JavaScript时间戳(如1620000000000),你的迁移类CreateUsersTable0000000000001使用自定义序号,不符合规则。

解决方案

  1. 重新生成合规迁移文件
    使用TypeORM CLI生成迁移,自动生成带时间戳的类名:

    npx typeorm-ts-node-esm migration:generate ./src/database/migrations/CreateUsersTable -d ./src/database/AppDataSource.ts
    

    生成后的类名应为类似CreateUsersTable1699999999999,文件名对应1699999999999-create-users-table.ts。

  2. 更新DataSource中的迁移引用
    修改AppDataSource.ts中的迁移导入,确保和新生成的类名一致:

    import { CreateUsersTable1699999999999 } from '~/database/migrations/1699999999999-create-users-table';
    
    export const AppDataSource = new DataSource({
      // ...其他配置
      migrations: [
        CreateUsersTable1699999999999,
      ],
    });
    

iOS循环依赖问题补充

当使用@ManyToOne(() => User)出现循环依赖时,本质是实体间双向引用导致打包时循环导入,解决方案:

  • 采用问题一中的表名字符串引用方案
  • 若必须使用箭头函数,可尝试延迟导入,但不推荐,会增加元数据解析复杂度

构建配置关键检查点

  1. tsconfig.json:确保emitDecoratorMetadata和experimentalDecorators始终为true,CodePush构建时不会覆盖该配置
  2. babel.config.js:@babel/plugin-proposal-decorators必须配置legacy: true,且module-resolver的alias和extensions完整覆盖TypeScript文件
  3. CodePush缓存清理:发布更新前清理本地缓存,避免旧元数据或迁移文件被打包:
    rm -rf node_modules/.cache
    npx react-native start --reset-cache
    

内容的提问来源于stack exchange,提问作者Ufuk Öncüler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 23:09:50