python - how to add object in database with sqlalchemy (doesn't insert) -
i'm trying add object in database function insert_data_into_the_picture_table when query database realize no object inserted.
class picture(timestampmixin, base): __tablename__ = 'picture' id = column(integer, primary_key=true) created_at = column(datetime) original_image_profile_hash = column(string(60)) normal_image_profile_hash = column(string(60)) profile_image_url = column(text) user_id_str = column(string(30)) user_id = column(integer, foreignkey('user.id')) user = relationship(user) def insert_data_into_the_picture_table(): beginning = datetime.now() compte = 0 open('user_pictures.json', encoding='utf-8') data_file: data = json.load(data_file) information in data[:10]: = datetime.now() twitter_user_id = information.get('twitter_user_id') user = session.query(user).filter(user.id_str == twitter_user_id).first() if user: print("found - {0}".format(twitter_user_id)) original_image_profile_hash = information.get('original_image_profile_hash') normal_image_profile_hash = information.get('normal_image_profile_hash') profile_image_url = information.get('profile_image_url') created_at = parse(information.get('created_at')) new_picture = picture( user=user, original_image_profile_hash = original_image_profile_hash, normal_image_profile_hash = normal_image_profile_hash, profile_image_url = profile_image_url, created_at = created_at ) session.add(new_picture) session.commit session.flush() compte += 1 import ipdb; ipdb.set_trace() else: #print("{0} not found: {1}".format('-'*3, twitter_user_id)) pass print("{0}/{1}: number of inserted document".format(compte, len(data))) print (timeago.format(beginning, now))
result (with debugger)
ipdb> !session.query(picture).count() 1 ipdb> !session.query(picture).first().id 42077 ipdb> !session.query(picture).first().original_image_profile_hash '000018ff7e3c0000' ipdb> !engine engine(mysql+pymysql://root:***@localhost/elizabeth?charset=utf8)
mysql
mysql> use elizabeth; database changed mysql> select * picture; empty set (0.00 sec) mysql> describe picture; +-----------------------------+-------------+------+-----+---------+----------------+ | field | type | null | key | default | | +-----------------------------+-------------+------+-----+---------+----------------+ | id | int(11) | no | pri | null | auto_increment | | created | datetime | yes | | null | | | created_at | datetime | yes | | null | | | original_image_profile_hash | varchar(60) | yes | | null | | | normal_image_profile_hash | varchar(60) | yes | | null | | | profile_image_url | text | yes | | null | | | user_id_str | varchar(30) | yes | | null | | | user_id | int(11) | yes | mul | null | | +-----------------------------+-------------+------+-----+---------+----------------+ 8 rows in set (0.00 sec)
Comments
Post a Comment