English 中文(简体)
我怎么能防止儿童重新融入到Kall Alchemy,一到许多关系?
原标题:How can I prevent children from being re-created in a SQLAlchemy one to many relationship?

我正在利用Salzak 2.6.6和Qalchemy0.66处理我数据库中一种与许多关系,并在存在类似数据的情况下,确保如何防止KallAlchemy增加新的儿童记录。

数据库代码:

from sqlalchemy import *
from sqlalchemy.orm import backref, relationship, sessionmaker, create_session
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

# Parent table, no foreign key.
class Author(Base):
    __tablename__ =  authors 

    id = Column(Integer, primary_key=True)
    username = Column(String)
    author_metadata = relationship( AuthorMetadata , backref= author )

# Child table, many records with same author_id.
class AuthorMetadata(Base):
    __tablename__ =  author_metadata 

    id = Column(Integer, primary_key=True)
    author_id = Column(Integer, ForeignKey( authors.id ))
    metakey = Column(String)
    metavalue = Column(Text)

例:

if __name__ ==  __main__ :
    engine = create_engine( database_details , pool_recycle=90)
    session = create_session(bind=engine)

    author = session.query(Author).filter_by(username= Godfrey ).first()
    if not author:
        author = Author()
    author.username =  Godfrey 
    author.author_metadata = [
        AuthorMetadata(metakey= location , metavalue= New York ),
        AuthorMetadata(metakey= posts , metavalue= 5 )]
    session.add(author)
    session.flush()

我第一次使用实例说明,以下内容出现在数据库中(预期):

dev=# select id from authors where username =  Godfrey ;
  id  
------
 5025
(1 row)

dev=# select id, author_id, metakey, metavalue from author_metadata order by id desc limit 2;
  id   | author_id | metakey  | metavalue 
-------+-----------+----------+-----------
 85090 |      5025 | posts    | 5
 85089 |      5025 | location | New York
(2 rows)

如果我再次使用实例说明,你可以看到,现有的元数据记录作者已经被定为无效,并插入了新的记录:

dev=# select id, author_id, metakey, metavalue from author_metadata order by id desc limit 4;
  id   | author_id | metakey  | metavalue 
-------+-----------+----------+-----------
 85092 |      5025 | posts    | 5
 85091 |      5025 | location | New York
 85090 |           | posts    | 5
 85089 |           | location | New York
(4 rows)

我没有发现这一点令人惊讶,但很想知道,如果新的元数据清单与现有清单不同,它是否只有插入/修改/替代作者元数据。

最佳回答

You could explicitely check the contents of the list and only append new AuthorMetadata objects if they don t exist, rather than delete the entire collection and re-create it with brand new objects. That would at least avoid discarding the previously created records.

页: 1 非常好,因此,你可能想与其中一人接触。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签