Insert different UUID on each row of a large table by python

Question:

15

I have a table with ~80k rows with imported data. Table structure is as follows:

order_line_items

  • id
  • order_id
  • product_id
  • quantity
  • price
  • uuid

On import, the order_id, product_id, quantity, and price were imported, but the uuid field was left null.

Is there a way, using python’s UUID() function, to add a uuid to each row of the table in bulk? I could use a script to cycle through each row and update it but if there is a python solution, that would be fastest.

Asked By: Ronak Patel

||

Answers:

A more Pythonic way in adding/modifying a value in a column is by using map method. You can find refer here for more details: https://pandas.pydata.org/docs/reference/api/pandas.Series.map.html.

Basically, what map is doing is map values of a column according to an function.

Your function must return a value for this to works, and you can take in the original value in the column as argument.

Answered By: kianyanglee

I’m fairly certain you can do this directly in MySQL using the UUID function.

UPDATE your_table_name SET uuid = UUID();
Answered By: Kaleb Fenley

Probably you need to add the default uuid for the table/model and and save value

from uuid import uuid4
from sqlalchemy import Column, String

class Table(Base):
    __tablename__ = 'table'

    id = Column(String, primary_key=True, default=uuid4)
    # add other column

records =[] # records in dict 
sess = session() # database session

# save all records in db
sess.bulk_insert_mappings(Table, records)
sess.commit()
Answered By: sahasrara62
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.