Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataMapper import primary key

I'm running an import script which imports a CSV dump of a database into a local sqlite database using DataMapper.

My models look like this:

class Staff

    include DataMapper::Resource

    property  :staff_id, String, :key => true
    property  :full_name, String

end

class Project

  include DataMapper::Resource

  property  :project_id, Integer, :key => true
  property  :title, String
  property  :status, String

 belongs_to  :staff

end

The CSV contains the primary key so when I'm do the import I'm using that as it's key. Next time I run the import I clear the tables and start again, however datamapper moans because the primary keys have already been taken.

Is there a way to stop datamapper moaning about this or should I just delete the .db file and re-create an empty .db file just before the import runs? If so what's the easiest way to do this.

like image 349
Tom Avatar asked Dec 14 '25 11:12

Tom


1 Answers

You can use DataMapper.auto_migrate! to blow away the tables, and then recreate them matching the current model state. The new tables will be empty of any data from previous runs.

So just after you define your models, but before you begin importing the data do something like the following:

DataMapper.finalize.auto_migrate!
like image 74
dkubb Avatar answered Dec 17 '25 02:12

dkubb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!