Note that there are some explanatory texts on larger screens.

plurals
  1. POSQLAlchemy context sensitive function for creating a composite value on insert and update
    primarykey
    data
    text
    <p>I'm trying to sort out how to add a context sensitive function (e.g. def get_user_full_name()) that will get triggered from the default and onupdate specifications on a column. I'm trying to set a composite field that combines the first and last names into a single full name value to avoid having to constantly concat the two fields in all queries.</p> <p>I'm using the declarative approach. This is an example of a similar model:</p> <pre><code>class SomeMembers(Base): __tablename__ = 'some_members' __table_args__ = {'mysql_engine':'InnoDB'} SomeGroup = Column(Unicode(50), primary_key=True) UserID = Column(Unicode(50), primary_key=True) FullName = Column(Unicode(255), default=get_user_full_name, onupdate=get_user_full_name, index=True) FirstName = Column(Unicode(255), index=True) LastName = Column(Unicode(255), index=True) UserName = Column(Unicode(255)) </code></pre> <p>This is the function that I wrote which reports in the stack trace that neither the FirstName or LastName keys are valid:</p> <pre><code>def get_user_full_name(context): return " ".join((context.compiled_parameters[0]['FirstName'],context.compiled_parameters[0]['LastName'])) </code></pre> <p>It isn't clear to me how I can reference the two columns (FirstName and LastName) existing data to create a composite value to store for easy retrieval. For example, if I had a user named John Doe the get_user_full_name method should take "John" and "Doe" and return a full name value of "John Doe". </p> <p>In the <a href="http://www.sqlalchemy.org/docs/metadata.html#context-sensitive-default-functions" rel="nofollow noreferrer">documentation</a> it refers to context.current_parameters but that does not exist in my case. I suspect it is because we use the declarative approach. If it did work my get_user_full_name function would look like:</p> <pre><code>def get_user_full_name(context): return " ".join((context.current_parameters['FirstName'],context.current_parameters['LastName'])) </code></pre> <p>I know this is probably trivial but I can't seem to crack the code on where this information is stored and how to access it dynamically.</p> <p>A little insight would be much appreciated. I'm also open to a more elegant approach too.</p> <p>Thanks in advance...</p> <p>Cheers,</p> <p>Paul</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload