I want to create a model for a mail service, each mail will have an author and a recipient, also each user must have sent and received messages
Here is my code
class User(Base, Find):
__tablename__ = "users"
id = Column(Integer, primary_key=True)
username = Column(String(30))
fullname = Column(String(50))
sent_mails = relationship("Mail", back_populates="author")
received_mails = relationship("Mail", back_populates="recipient")
def __repr__(self):
return f"User(id={self.id})"
class Mail(Base, Find):
__tablename__ = "mails"
id = Column(Integer, primary_key=True)
author_id = Column(Integer, ForeignKey("users.id"))
recipient_id = Column(Integer, ForeignKey("users.id"))
author = relationship("User", back_populates="sent_mails")
recipient = relationship("User", back_populates="received_mails")
def __repr__(self):
return f"Mail(id={self.id})"
When I try to use models I get an exception
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship User.sent_mails - there are multiple forei
gn key paths linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key refere
nce to the parent table.
What's my mistake?
Related
Context: I'm making an auctioning website for which I am using Flask-SQLAlchemy. My tables will need to have a many-to-many relationship (as one artpiece can have many user bids and a user can bid on many artpieces)
My question is: it is possible to add another column to my joining table to contain the id of the user bidding, the id of artpiece that they are bidding on and also how much they bid? Also if yes, how would I include this bid in the table when I add a record to said table?
bid_table = db.Table("bid_table",
db.Column("user_id", db.Integer, db.ForeignKey("user.user_id")),
db.Column("item_id", db.Integer, db.ForeignKey("artpiece.item_id"))
)
class User(db.Model):
user_id = db.Column(db.Integer, unique=True, primary_key=True, nullable=False)
username = db.Column(db.Integer, unique=True, nullable=False)
email = db.Column(db.String(50), unique =True, nullable=False)
password = db.Column(db.String(60), nullable=False)
creation_date = db.Column(db.DateTime, default=str(datetime.datetime.now()))
bids = db.relationship("Artpiece", secondary=bid_table, backref=db.backref("bids", lazy="dynamic"))
class Artpiece(db.Model):
item_id = db.Column(db.Integer, unique=True, primary_key=True, nullable=False)
artist = db.Column(db.String(40), nullable=False)
buyer = db.Column(db.String(40), nullable=False)
end_date = db.Column(db.String(40))
highest_bid = db.Column(db.String(40))
It is possible to do this with SQL Alchemy, but it's very cumbersome in my opinion.
SQLAlchemy uses a concept called an Association Proxy to turn a normal table into an association table. This table can have whatever data fields you want on it, but you have to manually tell SQLAlchemy which columns are foreign keys to the other two tables in question.
This is a good example from the documentation.
In your case, the UserKeyword table is the association proxy table that you want to build for your user/bid scenario.
The special_key column is the arbitrary data you would store like the bid amount.
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import backref, declarative_base, relationship
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String(64))
# association proxy of "user_keywords" collection
# to "keyword" attribute
keywords = association_proxy('user_keywords', 'keyword')
def __init__(self, name):
self.name = name
class UserKeyword(Base):
__tablename__ = 'user_keyword'
user_id = Column(Integer, ForeignKey('user.id'), primary_key=True)
keyword_id = Column(Integer, ForeignKey('keyword.id'), primary_key=True)
special_key = Column(String(50))
# bidirectional attribute/collection of "user"/"user_keywords"
user = relationship(User,
backref=backref("user_keywords",
cascade="all, delete-orphan")
)
# reference to the "Keyword" object
keyword = relationship("Keyword")
def __init__(self, keyword=None, user=None, special_key=None):
self.user = user
self.keyword = keyword
self.special_key = special_key
class Keyword(Base):
__tablename__ = 'keyword'
id = Column(Integer, primary_key=True)
keyword = Column('keyword', String(64))
def __init__(self, keyword):
self.keyword = keyword
def __repr__(self):
return 'Keyword(%s)' % repr(self.keyword)
Check out the full documentation for instructions on how to access and create this kind of model.
Having used this in a real project, it's not particularly fun and if you can avoid it, I would recommend it.
https://docs.sqlalchemy.org/en/14/orm/extensions/associationproxy.html
I have two classes like:
Base = declarative_base()
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False, unique = True)
class Order(Base):
__tablename__ = 'orders'
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey('Users.id'))
sent = Column(Boolean, default=False)
user = relationship('User', backref=backref('orders'))
def __init__(self, user=None, sent=False):
self.user = user
self.sent = sent
When I execute the following code :
u = session.query(User)[0]
o = Order()
u.orders.append(o)
session.commit()
SqlAlachemy issues an insert statement on the orders table. When I execute :
del u.orders[1]
session.commit()
sqlalchemy issues an update statement on the orders table setting user_id to NULL.
I need SqlAlchemy to issue a delete statement and the records to disappear from the database, not just being set to NULL. I've tried setting the cascade='save-update' keyword argument in the backref call, but did not succeed. I thing there must be some keyword argument configuration to achieve this, but I could not find it yet.
How could I reach the explained behaviour ? Namely, the issue of delete statements on the child tables?
I defined a role / permission model using the following model structure:
class Role(db.Model):
__tablename__ = 'roles'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(120), unique=True, nullable=False)
permissions = db.relationship("Permission", secondary=Role2Permission.__tablename__)
#classmethod
def find_by_id(cls, id):
return cls.query.filter_by(id=id).first()
class Role2Permission(db.Model):
__tablename__ = 'role_2_permission'
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'), primary_key=True)
permission_id = db.Column(db.Integer, db.ForeignKey('permissions.id'), primary_key=True)
bool_value = db.Column(db.Boolean, default=False)
class Permission(db.Model):
__tablename__ = 'permissions'
id = db.Column(db.Integer, primary_key=True)
action = db.Column(db.String(255))
role2permission = db.relationship('Role2Permission', lazy='joined')
bool_value = association_proxy('role2permission', 'bool_value')
When I fetch a role I want to have the permission value (bool_value) to be set via the association proxy coming from the association table. However using cls.query.filter_by(id=id).first() (where cls is the Role) returns the wrong bool_values for the queried role. I think the reason can be seen when looking at the generated SQL:
SELECT permissions.id AS permissions_id,
permissions.action AS permissions_action,
role_2_permission_1.role_id AS role_2_permission_1_role_id,
role_2_permission_1.permission_id AS role_2_permission_1_permission_id,
role_2_permission_1.bool_value AS role_2_permission_1_bool_value
FROM role_2_permission,
permissions
LEFT OUTER JOIN role_2_permission AS role_2_permission_1 ON permissions.id = role_2_permission_1.permission_id
WHERE 1 = role_2_permission.role_id AND permissions.id = role_2_permission.permission_id
I think this is fetching too many rows because it's selecting from the permissions table instead of just joining it to the role_2_permission table but then for some reason joining role_2_permission again. Somehow flask / sqlalchemy is then reducing the returned rows in a bad way: It's not actually so instead of the bool_values that belong to e.g. role 1, it returns the bool_values that belong to role 2.
How do I have to fix my model to get the correct permission data when querying the role?
I am trying to use an autoincrementing unique ID field as a foreign key in other tables. My model is below:
class User(Base):
__tablename__ = 'Users'
uid = Column(INT, primary_key=True, autoincrement=True)
name = Column(TEXT)
email = Column(TEXT)
dateRegistered = Column(TIMESTAMP)
phone = Column(TEXT)
class AddressMap(Base):
__tablename__ = 'AddressMaps'
uid = Column(INT, primary_key=True, autoincrement=True)
userId = Column(INT, ForeignKey('Users.uid'))
addressId = Column(INT, ForeignKey('Addresses.uid'))
dateCreated = Column(TIMESTAMP)
user = relationship("User", backref=backref('AddressMaps'))
address = relationship("Address", backref=backref('AddressMaps'))
class Address(Base):
__tablename__ = 'Addresses'
uid = Column(INT, primary_key=True, autoincrement=True)
street = Column(TEXT)
city = Column(TEXT)
state = Column(TEXT)
postal = Column(TEXT)
dateRegistered = Column(TIMESTAMP)
My problem is that when I create a user object, it is not created with a uid value. I understand this is because the object has not yet been committed to the database. The problem is that since the User object has None as the uid value, I have no way of linking the AddressMap object with it.
What is the idiomatic way of handling this problem in SQLAlchemy?
You shouldn't need a uid. Since you have created a relationship between the tables, you will be able to add an AddressMap object to the user:
samantha = User(name='Sam', email='sam#xyz.abc', phone='555-555-5555')
You now have access to the samantha.AddressMaps collection (see your backref under your user relationship in your AddressMap table). You can add AddressMap objects to this collection:
samantha.AddressMaps = [AddressMap(dateCreated=datetime.now()),
AddressMap(dateCreated=datetime.min)]
Now you can add these objects to your session and commit. See the docs for more info.
Incidentally, just as an FYI, you don't need to include autoincrement=True on the first integer column of a table.
Hi im having some trouble with foreign key in sqlalchemy not auto incrementing on a primary key ID
Im using: python 2.7, pyramid 1.3 and sqlalchemy 0.7
Here is my models
class Page(Base):
__tablename__ = 'page'
id = Column(Integer, ForeignKey('mapper.object_id'), autoincrement=True, primary_key=True)
title = Column(String(30), unique=True)
title_slug = Column(String(75), unique=True)
text = Column(Text)
date_added = Column(DateTime)
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String(100), unique=True)
email = Column(String(100), unique=True)
password = Column(String(100))
class Group(Base):
__tablename__ = 'groups'
id = Column(Integer, primary_key=True)
name = Column(String(100), unique=True)
class Member(Base):
__tablename__ = 'members'
user_id = Column(Integer, ForeignKey('user.id'), primary_key=True)
group_id = Column(Integer, ForeignKey('groups.id'), primary_key=True)
class Resource(Base):
__tablename__ = 'resource'
id = Column(Integer, primary_key=True)
tablename = Column(Text)
action = Column(Text)
class Mapper(Base):
__tablename__ = 'mapper'
resource_id = Column(Integer, ForeignKey('resource.id'), primary_key=True)
group_id = Column(Integer, ForeignKey('groups.id'), primary_key=True)
object_id = Column(Integer, primary_key=True)
and here is my RAW SQL query which i've written in SQLAlchemys ORM
'''
SELECT g.name, r.action
FROM groups AS g
INNER JOIN resource AS r
ON m.resource_id = r.id
INNER JOIN page AS p
ON p.id = m.object_id
INNER JOIN mapper AS m
ON m.group_id = g.id
WHERE p.id = ? AND
r.tablename = ?;
'''
obj = Page
query = DBSession().query(Group.name, Resource.action)\
.join(Mapper)\
.join(obj)\
.join(Resource)\
.filter(obj.id == obj_id, Resource.tablename == obj.__tablename__).all()
the raw SQL Query works fine without any relations between Page and Mapper, but SQLAlchemys ORM seem to require a ForeignKey link to be able to join them. So i decided to put the ForeignKey at Page.id since Mapper.object_id will link to several different tables.
This makes the SQL ORM query with the joins work as expected but adding new data to the Page table results in a exception.
FlushError: Instance <Page at 0x3377c90> has a NULL identity key.
If this is an auto- generated value, check that the database
table allows generation of new primary key values, and that the mapped
Column object is configured to expect these generated values.
Ensure also that this flush() is not occurring at an inappropriate time,
such as within a load() event.
here is my view code:
try:
session = DBSession()
with transaction.manager:
page = Page(title, text)
session.add(page)
return HTTPFound(location=request.route_url('home'))
except Exception as e:
print e
pass
finally:
session.close()
I really don't know why, but i'd rather have the solution in SQLalchemy than doing the RAW SQL since im making this project for learning purposes :)
I do not think autoincrement=True and ForeignKey(...) play together well.
In any case, for join to work without any ForeignKey, you can just specify the join condition in the second parameter of the join(...):
obj = Page
query = DBSession().query(Group.name, Resource.action)\
.join(Mapper)\
.join(Resource)\
.join(obj, Resource.tablename == obj.__tablename__)\
.filter(obj.id == obj_id)\
.all()