Question

J'ai une application de rails et je suis en train de charger des données dans via la commande COPY de PostgreSQL. Le fichier est CSV. Le tableau des cartes à un modèle Rails et je dois conserver la colonne de clé primaire ID afin que je puisse utiliser model.for_each pour jouer avec les données -. La table sera 5M + lignes

Ma question est, comment puis-je structurer mon CSV afin que je puisse charger des données dans la table et permettent encore la colonne ID d'être là? C'est une déception parce que la seule raison pour laquelle je en ai besoin est pour la méthode for_each.

Peu importe, j'essayé d'envoyer NULLs, comme dans:

NULL,col1,col2,col3,col4, etc.

Mais il ne fonctionne pas.

Merci.

Était-ce utile?

La solution

Passing in null for the primary key will never work no matter what options you have set for null string. You would be telling the backend to set the primary key to null which it will never allow no matter what the command to insert might be.

I really have no idea what you mean by retaining the primary key. That is something that is going be retained no matter what do. If you mean letting the DB pick the value for you and the primary key is a serial (auto-increment) then explicitly name all the columns but the primary key:

COPY country (colA, colB, colC) FROM '/usr1/proj/bray/sql/country_data'; -- leave out pkey

It also might be quicker to read the documentation on what null string options you would like to use instead of guessing possible values:

http://www.postgresql.org/docs/9.0/static/sql-copy.html

Autres conseils

The default when using WITH CSV is an unquoted empty string, such as:

,col1,col2,col3,,col5

Which would create a record that looks like:

NULL,'col1','col2','col3',NULL,'col5'
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top