I have defined two model with relation between them as below:
class Role(db.Model):
__tablename__ = 'roles'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(40), nullable=False, unique=False)
db.relationship('User', backref='role', lazy='dynamic')
def __init__(self, name):
self.name = name
def __repr__(self):
return f'<Role id={self.id} name={self.name}>'
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(40), unique=True)
password = db.Column(db.String(40), unique=True)
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
def __init__(self, username, password, role_id):
self.username = username
self.password = password
self.role_id = role_id
def __repr__(self):
return f'<User id={self.id} username={self.username} password={self.password}>'
Then inside shell I created entries as below:
> admin_role = Role('Admin')
> db.session.add(admin_role)
> db.session.commit()
> admin_user = User('adminusername', 'adminpassword',admin_role.id)
> db.session.add(admin_user)
> db.session.commit()
When I try to query model I get perfect result:
>>> admin_role = Role.query.first()
>>> admin_user = User.query.first()
>>> print(admin_role)
<Role id=1 name=Admin>
>>> print(admin_user)
<User id=1 username=adminusername password=adminpassword>
But when I try to access relation
print(admin_role.users)
print(admin_user.role)
I get errors Role object has no attribute users and User object has no attribute role respectively.
Typo? You have to assign db.relationship() instance to a variable.
class Role(db.Model):
__tablename__ = 'roles'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(40), nullable=False, unique=False)
- db.relationship('User', backref='role', lazy='dynamic')
+ users = db.relationship('User', backref='role', lazy='dynamic')
Related
I am designing a database where user is super class and Customer and Admin are inheriting User class. So according to the documentation in SQL SQLAlchemy I define like this
User Class
class User(Base):
__tablename__ = 'user'
username = Column(String(40), primary_key=True)
is_verified = Column(Boolean, default=True)
type = Column(String(20), nullable=True)
password = Column(String(40))
first_name = Column(String(50), nullable=True)
last_name = Column(String(50), nullable=True)
image_url = Column(String(250), nullable=True)
email = Column(String(100), nullable=True)
phone = Column(String(15), nullable=True)
addresses = relationship("Address", back_populates="user")
customer = relationship("Customer", uselist=False, back_populates="user")
admin = relationship("Admin", uselist=False, back_populates="user")
def hash_password(self, password):
self.password = pwd_context.encrypt(password)
def verify_password(self, password):
return pwd_context.verify(password, self.password)
Customer Class
class Customer(Base):
__tablename__ = 'customer'
id = Column(Integer, primary_key=True)
username = Column(String(40), ForeignKey('user.username'))
user = relationship("User", back_populates="customer")
products = relationship("Review")
cart = relationship("Cart", uselist=False, back_populates="customer")
Admin Class
class Admin(Base):
__tablename__ = 'admin'
id = Column(Integer, primary_key=True)
username = Column(String(20), ForeignKey('user.username'))
user = relationship("User", back_populates="admin")
The problem is User and Customer are one to one , User and Admin are one to one . but I defied admin and customer in the user class.
Further when I delete user it's not deleting the customer automatically.
What is the proper way to implement this scenario ?
use cascade="save-update, merge, delete"
The delete cascade indicates that when a “parent” object is marked for deletion, its related “child” objects should also be marked for deletion. If for example, we have a relationship User.addresses with delete cascade configured
class User(Base):
__tablename__ = 'user'
username = Column(String(40), primary_key=True)
is_verified = Column(Boolean, default=True)
type = Column(String(20), nullable=True)
password = Column(String(40))
first_name = Column(String(50), nullable=True)
last_name = Column(String(50), nullable=True)
image_url = Column(String(250), nullable=True)
email = Column(String(100), nullable=True)
phone = Column(String(15), nullable=True)
addresses = relationship("Address", back_populates="user")
customer = relationship("Customer", uselist=False, back_populates="user",cascade="save-update, merge, delete")
admin = relationship("Admin", uselist=False, back_populates="user",cascade="save-update, merge, delete")
def hash_password(self, password):
self.password = pwd_context.encrypt(password)
def verify_password(self, password):
return pwd_context.verify(password, self.password)
This is my code:
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////root/Desktop/Social_Network/users.db'
app.config['SQLALCHEMY_BINDS'] = {'posts': 'sqlite:////root/Desktop/Social_Network/posts.db'}
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(20), unique=True, nullable=False)
email = db.Column(db.String(40), unique=True, nullable=False)
password = db.Column(db.String, nullable=False)
joined_at = db.Column(db.DateTime(),default =datetime.datetime.now)
is_hero = db.Column(db.Boolean(),default=False)
class Post(db.Model):
__bind_key__ = 'posts'
id = db.Column(db.Integer, primary_key=True)
user = db.Column(db.String(50))
post = db.Column(db.String(255))
score = db.Column(db.Integer, nullable=False)
downVotes = db.Column(db.Integer, nullable=False )
posted_time = db.Column(db.DateTime(),default =datetime.datetime.now)
upVotes = db.Column(db.Integer,nullable=False)
dict_ = {}
def get_all_users():
users_ = User.query.all()
global dict_
for user in users_:
dict_[user.email] = user.password
return dict_
I have connected multiple databases in SQLAlchemy. My problem is whenever I run :
users_ = User.query.all()
. It returns the User object, but after I run the change it to post, something like this:
post = Post.query.all()
It returns none. Any help regarding this issue?
I have the following badge (achievement) system database structure:
class Base(db.Model):
__abstract__ = True
id = db.Column(db.Integer, primary_key=True)
date_created = db.Column(db.DateTime, default=db.func.current_timestamp())
date_modified = db.Column(db.DateTime, default=db.func.current_timestamp(),
onupdate=db.func.current_timestamp())
class User(UserMixin, Base):
__tablename__ = 'users'
username = db.Column(db.String(20), nullable=False, unique=True)
email = db.Column(db.String(50), nullable=False, unique=True)
password = db.Column(db.String(200), nullable=False)
name = db.Column(db.String(30), nullable=False)
badges = db.relationship('UserBadge', backref='ubadge',
lazy='dynamic')
class Badge(Base):
__tablename__ = 'badges'
name = db.Column(db.String(35), unique=True)
description = db.Column(db.String(300))
imgfile = db.Column(db.String(80))
badges = db.relationship('UserBadge', backref='badge',
lazy='dynamic')
def __repr__(self):
return '<Achievement: {} - {}>'.format(self.name, self.description)
class UserBadge(Base):
__tablename__ = 'userbadges'
user_id = db.Column(db.Integer(), db.ForeignKey('users.id'))
badge_id = db.Column(db.Integer(), db.ForeignKey('badges.id'))
def __repr__(self):
return '<Achievement: {} - {}>'.format(self.user_id, self.badge_id)
So i can return all the badges by a specific user, using:
ubadges = UserBadge.query.filter_by(user_id=user.id).all()
It returns:
[<Achievement: 1 - 1>]
But instead of 1 (user_id) and 1 (badge_id) i want to show the users.name and badges.name. How can i access those attributes?
In your UserBadge class, just use:
def __repr__(self):
return '<Achievement: {} - {}>'.format(self.ubadge.name, self.badge.name)
It has both properties because you set them up using backref in the other classes.
PS: You might need to change the User backref to user, and then use self.user.name int he function above
So I created a basic Flask-User app with multiple roles and datastructure like this:
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
active = db.Column('is_active', db.Boolean(), nullable=False, server_default='0')
first_name = db.Column(db.String(50), nullable=False, default='')
last_name = db.Column(db.String(50), nullable=False, default='')
email = db.Column(db.String(255), nullable=False, unique=True)
confirmed_at = db.Column(db.DateTime())
# Relationships
user_auth = db.relationship('UserAuth', uselist=False)
roles = db.relationship('Role', secondary='user_roles',
backref=db.backref('users', lazy='dynamic'))
def is_active(self):
return self.active
class UserAuth(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE'))
# User authentication information
username = db.Column(db.String(50), nullable=False, unique=True)
password = db.Column(db.String(255), nullable=False, default='')
# Relationships
user = db.relationship('User', uselist=False)
def hash_password(self, password):
self.password_hash = pwd_context.encrypt(password)
def verify_password(self, password):
return pwd_context.verify(password, self.password_hash)
class Role(db.Model):
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(50), unique=True)
class UserRoles(db.Model):
id = db.Column(db.Integer(), primary_key=True)
user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE'))
role_id = db.Column(db.Integer(), db.ForeignKey('role.id', ondelete='CASCADE'))
I try to show it in Flask-Admin:
class AdminView(ModelView):
def is_accessible(self):
access = current_user.is_authenticated and current_user.has_role('admin')
return access
admin = Admin(app, name='My Admin', template_mode='bootstrap3')
admin.add_view(AdminView(User, db.session))
admin.add_view(AdminView(UserAuth, db.session))
admin.add_view(AdminView(Role, db.session))
admin.add_view(AdminView(UserRoles, db.session))
Why It shows in such a strange manner:
And how to see and be able to edit user roles when editing a user?
You have not defined a suitable string representation for instances of your UserAuth class. A suitable example would be:
class UserAuth(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer(), db.ForeignKey('user.id', ondelete='CASCADE'))
# User authentication information
username = db.Column(db.String(50), nullable=False, unique=True)
password = db.Column(db.String(255), nullable=False, default='')
# Relationships
user = db.relationship('User', uselist=False)
def hash_password(self, password):
self.password_hash = pwd_context.encrypt(password)
def verify_password(self, password):
return pwd_context.verify(password, self.password_hash)
def __str__(self):
return unicode(self).encode('utf-8')
def __unicode__(self):
return self.username
This needs to be done for all your models.
I do have these 2 calsses als DB models trying to buidl a 1 to many relation:
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), index=True) #, unique=True)
firstname = db.Column(db.String(50))
lastname = db.Column(db.String(50))
bt_ids = db.relationship("BT", order_by="BT.id", backref="user")
class BT(db.Model):
__tablename__ = 'bt'
id = db.Column(db.Integer, primary_key=True)
bt_id = db.Column(db.String(255), unique=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
user = db.relationship("User", backref=db.backref('bt', order_by=id))
But I guess I do not really understand the way of setting up the relationships:
ArgumentError: Error creating backref 'user' on relationship 'User.bt_ids': property of that name exists on mapper 'Mapper|BT|bt'
Any ideas?
EDIT
I actually wanted to achive something like the example of SQLAlchemy
class User(Base):
__tablename__ = 'users'
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
name = Column(String(50))
fullname = Column(String(50))
password = Column(String(12))
**addresses = relationship("Address", order_by="Address.id", backref="user")**
def __repr__(self):
return "<User(name='%s', fullname='%s', password='%s')>" % (
self.name, self.fullname, self.password)
class Address(Base):
__tablename__ = 'addresses'
id = Column(Integer, primary_key=True)
email_address = Column(String, nullable=False)
user_id = Column(Integer, ForeignKey('users.id'))
**user = relationship("User", backref=backref('addresses', order_by=id))**
def __repr__(self):
return "<Address(email_address='%s')>" % self.email_address
There is already a FK relation between User and BT through user_id property so you can't create another relation named user in BT.
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(255), index=True) #, unique=True)
firstname = db.Column(db.String(50))
lastname = db.Column(db.String(50))
bts = db.relationship("BT", order_by="BT.id", backref="user")
class BT(db.Model):
__tablename__ = 'bt'
id = db.Column(db.Integer, primary_key=True)
bt_id = db.Column(db.String(255), unique=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
Now I got it...
This was wrong:
user = db.relationship("User", backref=db.backref('bt_ids', order_by=id))
Now it works!