Closed YKSing closed 5 years ago
I created a new expo project and just copy the example of "Animal"
Animal.js
import { SQLite } from 'expo'
import { BaseModel, types } from 'expo-sqlite-orm'
export default class Animal extends BaseModel {
constructor(obj) {
super(obj)
}
static database() { // Here should take out "get" or exception occurred
return Promise.resolve(SQLite.openDatabase('database.db'))
}
static get tableName() {
return 'animals'
}
static get columnMapping() {
return {
id: { type: types.INTEGER, primary_key: true },
name: { type: types.TEXT, not_null: true },
color: { type: types.TEXT },
age: { type: types.NUMERIC },
another_uid: { type: types.INTEGER, unique: true },
timestamp: { type: types.INTEGER, default: () => Date.now() }
}
}
}
App.js
import React from 'react';
import { View } from 'react-native';
import Animal from './Animal';
export default class App extends React.Component {
async componentDidMount() {
await Animal.createTable();
await Animal.create({
id: 1,
name: '', // <- empty string
color: 'Brown',
age: 2
});
const createdAnimal = await Animal.find(1);
console.log(createdAnimal);
}
render() {
return ( <View /> );
}
}
console.log Result
Animal {
"age": 2,
"another_uid": null,
"color": "Brown",
"id": 1,
"name": null, // not empty string
"timestamp": 1547518269573,
}
Here are more information
console.log(await Animal.find(1));
// return
Animal {
"age": 2,
"another_uid": null,
"color": "Brown",
"id": 1,
"name": null, // return null here which is incorrect
"timestamp": 1547520079013, // this value will change every time
}
console.log(await Animal.query());
// return
Array [
Object { // maybe this should instance of Animal object?
"age": 2,
"another_uid": null,
"color": "Brown",
"id": 1,
"name": "", // return empty string here which is correct
"timestamp": null, // should not be null here?
},
]
Dose the default means "SQL" default value when insert into sqlite? Or it is just a "Logically" default value for returning when SQL value is null?
Thanks for contribution, i will check it
Hi, thanks for contribution, it was fixed in v1.4.2.
Test now to check if it is as expected.
Could you provide your model code please?