On 10/25/2016 05:46 PM, Robert C wrote:
I am modeling a simple hierarchical database structure. My model is
designed as follows:

|

|classChatMessage(Base):
    __tablename__ ='chat_message'
    sender_id =Column(Integer,ForeignKey('user.id'),primary_key=True)
    receiver_id =Column(Integer,ForeignKey('user.id'),primary_key=True)
    text =Column(String(50))


classUser(Base):
    __tablename__ ='user'


    id =Column(Integer,primary_key=True)
    phone_number =Column(PhoneNumberType())
    type =Column(String(50))

    __mapper_args__ ={
        'polymorphic_identity':'user',
        'polymorphic_on':type
    }

    sent_messages =relationship(
        'ChatMessage',
        foreign_keys='ChatMessage.sender_id',
        backref='sending_user'
    )

    received_messages =relationship(
        'ChatMessage',
        foreign_keys='ChatMessage.sender_id',
        backref='sending_user'
    )


classInvitedUser(TBHUser):
    __tablename__ ='invited_user'
    id =Column(Integer,ForeignKey('user.id'),primary_key=True)

    __mapper_args__ ={
        'polymorphic_identity':'invited_user',
    }


classVerifiedUser(TBHUser):
    __tablename__ ='verified_user'
    id =Column(Integer,ForeignKey('user.id'),primary_key=True)

    __mapper_args__ ={
        'polymorphic_identity':'verified_user',
    }|

|
|
|


I'll give a brief breakdown of some structural business rules present in
my application. A single user is able to chat with anyone in their
contacts. If a user messages a friend who is not registered in the app,
the chat message will be sent and the receiving user will receive an
invitation to download the app. Invited users are stored inside the
'InvitedUser' table. After an invited user signs up, the 'InvitedUser'
instance will be deleted and will be replaced with a 'VerifiedUser'
instance. The purpose of the 'InvitedUser' entity is to persist received
chat messages prior to signup.

While deleting an InvitedUser from the database, I am receiving the
error below.


|

|AssertionError:Dependencyrule tried to blank-outprimary key column
'chat_message.receiver_id'on instance '<ChatMessage at 0x1044a9cf8>'|

|
|
|

It appears that SQLAlchemy is not allowing the deletion of an
InvitedUser in order to preserve integrity of the ChatMessages received
by the user. A simple solution would be to have one user class and
potentially a 'status' attribute, however, that did not seem optimal.


well your database would not allow it either, as ChatMessage is dependent on that row existing.

There's no functionality to change the class of an object in place, you can of course emit the DELETE and INSERT yourself of the invited_user and verified_user tables using Core constructs, then re-load the object. However in this case I'd likely keep the "invited" / "verified" part of this as a separate object related to User via one-to-one (same relational structure, just in Python we're using composition instead of inheritance). Because the "user" stays as the same identity.




Is there a specific way to tell SQLAlchemy to transfer an 'InvitedUser'
to 'VerifiedUser'? Possibly my models are overcomplicated and there is a
different way of solving this problem.

Thanks, Rob.

--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and
Verifiable Example. See http://stackoverflow.com/help/mcve for a full
description.
---
You received this message because you are subscribed to the Google
Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

--
SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to