barthofu / tscord

🤖 A fully-featured discord bot template written in Typescript, intended to provide a framework that's easy to use, extend and modify
https://tscord.discbot.app
MIT License
263 stars 21 forks source link

[Bug] DB Entity update process #76

Closed DMSavchik closed 1 year ago

DMSavchik commented 2 years ago

What happened?

After I added the new property to User entity named balance I expected it to go live with next boot up of the bot.

    @Property()
    balance: number = 100

On other hand it gave me the error first saying that column is not present, after investigation got an adivse to make migration for DB

npm run migration:create && nom run migration:up"

Which brough up an error:

alter table `user` add column `balance` integer not null; - SQLITE_ERROR: Cannot add a NOT NULL column with default value NULL

then we found that this can be fixed by changing the way it described in User Entiry

    @Property({ default: 100 })
    balance: number

Which is works nice.

Would be good to iclude this in the Documentation for visibility

Reproduction

add new property

    @Property()
    balance: number = 100

and try to migrate with sqlite as your database

Relevant log output

alter table `user` add column `balance` integer not null; - SQLITE_ERROR: Cannot add a NOT NULL column with default value NULL

Code of Conduct

ElMehdiBouamama commented 1 year ago

You probably wont need to create new entities by yourself. The discordx implement an EAV design pattern that allows you to store key/(strongly typed)value pairs without having to create entities, so it would be better to use the native EAV pattern provided by the discordx team for this purpose instead of creating your own entities, since it is more suitable for discord usage!

I would not recommend going through the headache of creating your own own entities in the database and defining their relationships. Headache solved with the provided EAV pattern! 👍

For more information about the implemented EAV pattern check the discordx documentation bellow: https://barthofu.github.io/tscord-docs/docs/bot/features/database/single-data-type