i create a scripts to add data into my database with sqlmoel, i've 2 table one posts and the other forums, for the posts script worked normally without any problem but the forum script it return an error that the table forums is not mapped.
here is my code and error:
- forums model
from typing import Optional
from sqlmodel import Field, SQLModel
class Phpbb_forums(SQLModel, Table=True):
__tablename__ = 'phpbb_forums'
forum_id: Optional[int] = Field(default=None, primary_key=True)
parent_id: int = '0'
left_id: Optional[int] = Field(default=None, primary_key=True)
right_id: Optional[int] = Field(default=None, primary_key=True)
forum_parents: str
forum_name: str
forum_desc: str
forum_desc_bitfield: str = ''
forum_desc_options: int = '7'
forum_desc_uid: str = ''
forum_link: str = ''
forum_password: str = ''
forum_style: int = '0'
forum_image: str = ''
forum_rules: str = ''
forum_rules_link: str = ''
forum_rules_bitfield: str = ''
forum_rules_options: int = '7'
forum_rules_uid: str = ''
forum_topics_per_page: int = '0'
forum_type: int = '0'
forum_status: int = '0'
forum_last_post_id: Optional[int] = Field(default=None, primary_key=True)
forum_last_poster_id: int = '0'
forum_last_post_subject: str = ''
forum_last_post_time: int = '0'
forum_last_poster_name: str = ''
forum_last_poster_colour: str = ''
forum_flags: int = '32'
display_on_index: int = '1'
enable_indexing: int = '1'
enable_icons: int = '1'
enable_prune: int = '0'
prune_next: int = '0'
prune_days: int = '0'
prune_viewed: int = '0'
prune_freq: int = '0'
display_subforum_list: int = '1'
display_subforum_limit: int = '0'
forum_options: int = '0'
enable_shadow_prune: int = '0'
prune_shadow_days: int = '7'
prune_shadow_freq: int = '1'
prune_shadow_next: int = '0'
forum_posts_approved: int = '0'
forum_posts_unapproved: int = '0'
forum_posts_softdeleted: int = '0'
forum_topics_approved: int = '0'
forum_topics_unapproved: int = '0'
forum_topics_softdeleted: int = '0'
- script to add to database
from sqlmodel import Session
from models.forumModels import Phpbb_forums
from db import engine
parent_id = 4 # id of categorie series and '6' for films
parent_name = 'Series' # name of categorie Series and 'Films' for films
def create_forum():
forum = Phpbb_forums(
# parent_id=parent_id,
forum_parents=parent_name,
forum_name='vikings',
forum_desc='la serie de vikings '
)
# with Session(engine) as session:
session = Session(engine)
if session:
print("session:", session)
try:
session.add(forum)
print("success adding")
except Exception as e:
session.rollback()
print("error:", str(e))
try:
session.commit()
print("success commit")
except Exception as e:
session.rollback()
print("error:", str(e))
def main():
create_forum()
if __name__ == "__main__":
main()
and this is my table in database
CREATE TABLE `phpbb_forums` (
`forum_id` MEDIUMINT(8) UNSIGNED NOT NULL AUTO_INCREMENT,
`parent_id` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`left_id` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`right_id` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`forum_parents` MEDIUMTEXT NOT NULL COLLATE 'utf8mb3_bin',
`forum_name` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8mb3_bin',
`forum_desc` TEXT NOT NULL COLLATE 'utf8mb3_bin',
`forum_desc_bitfield` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8mb3_bin',
`forum_desc_options` INT(11) UNSIGNED NOT NULL DEFAULT '7',
`forum_desc_uid` VARCHAR(8) NOT NULL DEFAULT '' COLLATE 'utf8mb3_bin',
`forum_link` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8mb3_bin',
`forum_password` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8mb3_bin',
`forum_style` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`forum_image` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8mb3_bin',
`forum_rules` TEXT NOT NULL COLLATE 'utf8mb3_bin',
`forum_rules_link` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8mb3_bin',
`forum_rules_bitfield` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8mb3_bin',
`forum_rules_options` INT(11) UNSIGNED NOT NULL DEFAULT '7',
`forum_rules_uid` VARCHAR(8) NOT NULL DEFAULT '' COLLATE 'utf8mb3_bin',
`forum_topics_per_page` SMALLINT(4) UNSIGNED NOT NULL DEFAULT '0',
`forum_type` TINYINT(4) NOT NULL DEFAULT '0',
`forum_status` TINYINT(4) NOT NULL DEFAULT '0',
`forum_last_post_id` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`forum_last_poster_id` INT(10) UNSIGNED NOT NULL DEFAULT '0',
`forum_last_post_subject` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8mb3_bin',
`forum_last_post_time` INT(11) UNSIGNED NOT NULL DEFAULT '0',
`forum_last_poster_name` VARCHAR(255) NOT NULL DEFAULT '' COLLATE 'utf8mb3_bin',
`forum_last_poster_colour` VARCHAR(6) NOT NULL DEFAULT '' COLLATE 'utf8mb3_bin',
`forum_flags` TINYINT(4) NOT NULL DEFAULT '32',
`display_on_index` TINYINT(1) UNSIGNED NOT NULL DEFAULT '1',
`enable_indexing` TINYINT(1) UNSIGNED NOT NULL DEFAULT '1',
`enable_icons` TINYINT(1) UNSIGNED NOT NULL DEFAULT '1',
`enable_prune` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`prune_next` INT(11) UNSIGNED NOT NULL DEFAULT '0',
`prune_days` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`prune_viewed` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`prune_freq` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`display_subforum_list` TINYINT(1) UNSIGNED NOT NULL DEFAULT '1',
`display_subforum_limit` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`forum_options` INT(20) UNSIGNED NOT NULL DEFAULT '0',
`enable_shadow_prune` TINYINT(1) UNSIGNED NOT NULL DEFAULT '0',
`prune_shadow_days` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '7',
`prune_shadow_freq` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '1',
`prune_shadow_next` INT(11) NOT NULL DEFAULT '0',
`forum_posts_approved` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`forum_posts_unapproved` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`forum_posts_softdeleted` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`forum_topics_approved` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`forum_topics_unapproved` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
`forum_topics_softdeleted` MEDIUMINT(8) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`forum_id`) USING BTREE,
INDEX `left_right_id` (`left_id`, `right_id`) USING BTREE,
INDEX `forum_lastpost_id` (`forum_last_post_id`) USING BTREE
)
COLLATE='utf8mb3_bin'
ENGINE=InnoDB
AUTO_INCREMENT=9
;
- the error:
session: <sqlmodel.orm.session.Session object at 0x000002105BB29450>
error: Class 'models.forumModels.Phpbb_forums' is not mapped
success commit
where is the problem?
I'm still new my knowledge is limited please help me.