Делал по офф. документации.
Версия - 1.3.20
Ошибка:
Traceback (most recent call last):
File "C:\Users\user\Documents\GitHub\pythonProject\LifeSimulator\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2012, in add
state = attributes.instance_state(instance)
AttributeError: 'User' object has no attribute '_sa_instance_state'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "C:/Users/user/Documents/GitHub/pythonProject/LifeSimulator/src/tests/test1.py", line 102, in <module>
session.add(vasiaUser)
File "C:\Users\user\Documents\GitHub\pythonProject\LifeSimulator\venv\lib\site-packages\sqlalchemy\orm\session.py", line 2014, in add
util.raise_(
File "C:\Users\user\Documents\GitHub\pythonProject\LifeSimulator\venv\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
raise exception
sqlalchemy.orm.exc.UnmappedInstanceError: Class '__main__.User' is not mapped
Process finished with exit code 1
Код:
import random
from src.help import for_token
'''from sqlalchemy.orm import mapper #достать "Отобразитель" из пакета с объектно-реляционной моделью
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
metadata = MetaData()
engine = create_engine('sqlite:///database.sqlite3', echo=True)
users_table = Table('users', metadata,
Column('idd', Integer, primary_key=True),
Column('id', Integer),
Column('uid', Integer),
Column('nick', String(10)),
Column('balance', Integer),
Column('level', Integer),
Column('where', Integer),
Column('token', String),
Column('admin', Integer),
Column('moderator', Integer),
Column('donate', Integer),
)
class User(object):
__tablename__ = 'users'
Column('idd', Integer, primary_key=True)
Column('id', Integer)
Column('uid', Integer)
Column('nick', String(10))
Column('balance', Integer)
Column('level', Integer)
Column('where', Integer)
Column('token', String)
Column('admin', Integer)
Column('moderator', Integer)
Column('donate', Integer)
def __init__(self, id, uid, nick, balance, level, where, token, admin, moderator, donate):
self.user = f"<User({id}, {uid}, '{nick}', {balance}, {level}, '{where}', '{token}', {admin}, {moderator}, {donate})>"
def __repr__(self):
return self.user
Base.metadata.create_all(engine)
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()
metadata = Base.metadata
a = ''
for i in range(60):
a += random.choice(for_token)
vasiaUser = User(549204433, 0, 'Timtaran', 999999999, 99999999, 'start', a, 1, 1, 0)
session.add(vasiaUser)
session.commit()'''
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///database.sqlite3', echo=True)
from sqlalchemy import Table, Column, Integer, String, MetaData, ForeignKey
Base = declarative_base()
metadata = MetaData()
users_table = Table('users', metadata,
Column('idd', Integer, primary_key=True),
Column('id', Integer),
Column('uid', Integer),
Column('nick', String(10)),
Column('balance', Integer),
Column('level', Integer),
Column('where', Integer),
Column('token', String),
Column('admin', Integer),
Column('moderator', Integer),
Column('donate', Integer),
)
class User(object):
__tablename__ = 'users'
Column('idd', Integer, primary_key=True)
Column('id', Integer)
Column('uid', Integer)
Column('nick', String(10))
Column('balance', Integer)
Column('level', Integer)
Column('where', Integer)
Column('token', String)
Column('admin', Integer)
Column('moderator', Integer)
Column('donate', Integer)
def __init__(self, id, uid, nick, balance, level, where, token, admin, moderator, donate):
self.user = f"<User({id}, {uid}, '{nick}', {balance}, {level}, '{where}', '{token}', {admin}, {moderator}, {donate})>"
def __repr__(self):
return self.user
# Создание таблицы
Base.metadata.create_all(engine)
from sqlalchemy.orm import sessionmaker
Session = sessionmaker(bind=engine)
session = Session()
a = ''
for i in range(60):
a += random.choice(for_token)
vasiaUser = User(549204433, 0, 'Timtaran', 999999999, 99999999, 'start', a, 1, 1, 0)
session.add(vasiaUser)
session.commit()
users = 0
for row in session.query(User, User.nick).all():
print(row.User)
users += 1
print(users)