Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.62k stars 600 forks source link

no such table: demands (code 1 SQLITE_ERROR): #1557

Open brunosp-49 opened 1 year ago

brunosp-49 commented 1 year ago

Hi, I'm trying to fetch all the data of my collection called demands, but I can't do that because I receive this error no such table: demands (code 1 SQLITE_ERROR): that is my code: const result = await database.collections.get('demands').query().fetch(); and that is my database:

import {Database} from '@nozbe/watermelondb';
import SQLiteAdapter from '@nozbe/watermelondb/adapters/sqlite';
import schema from './schema';
import Demands from './schemas/demands';
import Users from './schemas/users';

const adapter = new SQLiteAdapter({
  dbName: 'Espeleologia',
  schema,
});

export const database = new Database({
    adapter,
    modelClasses: [
        Users,
        Demands
    ],
})

and my schema:

import { appSchema } from '@nozbe/watermelondb';
import { demandsSchema } from './schemas/demands';
import { usersSchema } from './schemas/users';

export default appSchema({
    version: 1,
    tables: [
        usersSchema,
        demandsSchema
    ]
})

and finally my "demand" schema part:

import {Model, tableSchema} from '@nozbe/watermelondb';
import {field} from '@nozbe/watermelondb/decorators';

export default class Demands extends Model {
  static table = 'demands';

  @field('demanda_id') demanda_id!: number;
  @field('nome_projeto') nome_projeto!: string;
  @field('codigoOs') codigoOs!: string;
  @field('demanda') demanda!: boolean;
  @field('codigoOsBrandt') codigoOsBrandt!: string;
  @field('dataInicialOs') dataInicialOs!: string;
  @field('dataTerminoContrato') dataTerminoContrato!: string;
  @field('zonaUtm') zonaUtm!: string;
  @field('datum') datum!: string;
  @field('nomeFiscal') nomeFiscal!: string;
  @field('emailFiscal') emailFiscal!: string;
  @field('telefoneFiscal') telefoneFiscal!: string;
  @field('contrato') contrato!: string;
  @field('responsavelBrandt') responsavelBrandt!: string;
  @field('escopo') escopo!: string;
}

export const demandsSchema = tableSchema({
  name: 'demands',
  columns: [
    {name: 'demanda_id', type: 'number'},
    {name: 'nome_projeto', type: 'string'},
    {name: 'codigoOs', type: 'string'},
    {name: 'demanda', type: 'boolean'},
    {name: 'codigoOsBrandt', type: 'string'},
    {name: 'dataInicialOs', type: 'string'},
    {name: 'dataTerminoContrato', type: 'string'},
    {name: 'zonaUtm', type: 'string'},
    {name: 'datum', type: 'string'},
    {name: 'nomeFiscal', type: 'string'},
    {name: 'emailFiscal', type: 'string'},
    {name: 'telefoneFiscal', type: 'string'},
    {name: 'contrato', type: 'string'},
    {name: 'responsavelBrandt', type: 'string'},
    {name: 'escopo', type: 'string'},
  ],
});

how can I make this work?

radex commented 1 year ago

Did you bump schema version after changing schema to force it to be recreated?

brunosp-49 commented 1 year ago

Yes I tried, thank you @radex , I found the solution, and maybe that can help someone else, I had to reorganize my schemas, and delete the dbName in my database, then it worked

yakusho commented 1 year ago

Changing the dbName works because your database will be rebuilt using the new name and create the tables again, if you create new tables later on you'll need to clear your app storage, reinstall the app, bump the schema version and create the migrations (if you already have the app running in prod) or change the dbName again (which is not really optimal).

chanphiromsok commented 1 year ago

works

resolved my issued +1