python - Checking db-models on uniqueness. INSERT OR IGNORE by means of Flask-SQLAlchemy -


I have a question about how to add new but unique items to the database.

  app = Flask (__ Nam__, Templet_folder = 'template') app.config.from_object ( 'config') db = SQLAlchemy (app)   

classes this many-to-see several implementations:

  connection = db.Table ( 'connections', db.metadata, db.Column ( 'book_id', db.Integer, db .ForeignKey ( 'books.id')), dB. column ( 'author_id', db.Integer, db.ForeignKey ( 'authors.id'))) class author (dB. model): __tablename__ = 'authors' __searchable__ = [ 'a_name'] __table_args__ = { 'sqlite_autoincrement': truth id} = db.Column (db.Integer, primary_key = true) a_name = db.Column (db.String (80), unique = true) def __repr __ (self ): Return Unicode (self.a_name) class Ustk (dB. Model): __tablename__ = 'books' __searchable__ = [ 'b_name'] __table_args__ = { 'sqlite_autoincrement'}: true} id = dB. Columns (DB Integer, Primary_Keys = True) B_Name = DB Columns (db string (80), unique = true) author = db. R ( 'author', secondary = lambda connection, Bakf = Dbkbackref ( 'books')) def __repr __ (self): return Unicode (self.b_name)   

I want Only add unique DB items if I will write this code:

  author = author (a_name = "author1") book = book (b_name = "Book1") author Bookss.append (book) db.session.add (author) db.session.add (book) db.session.commit ()   

And in our database we already have a_name The author of "Author1", the error would be the proclamation.

  Intigritiarrontr: (Intigritiarrr) column a_name authors (A_nam) values ​​(?) '(' '\ U0410 \ u0432 \ u0442 \ u043e \ u04402' ') is not unique Uaianaarti   

Do I need to check the specificity of this Insurheus and how? Or is there another best solution?

ORM is not supported for "merge" approach (like "INSERT or REPLACE" and all of these) And there are many complications together. I use the recipe or some version of it. When I'm merging a bigger data, then I load a whole set of items for a particular part in a dictionary some time ago, so they only have one SELECT statement to load, so check in the bus. As the dictionary I proceed.

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -