Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

649
Views
¿Postgres y Laravel cómo cambiar la columna de tipo cadena a entero?

Estoy tratando de cambiar una columna de tipo cadena a entero en Postgres y Laravel 6.x. He intentado hacer esto con una migración como esta:

 public function up() { Schema::table('job_listings', function (Blueprint $table) { $table->integer('company_id')->change(); }); }

Cuando ejecuto esta migración, aparece un error de que la columna no se puede convertir automáticamente en un número entero:

 In Connection.php line 664: SQLSTATE[42804]: Datatype mismatch: 7 ERROR: column "company_id" cannot be cast automatically to type integer HINT: You might need to specify "USING company_id::integer". (SQL: ALTER TABLE job_listings ALTER company_id TYPE INT) In PDOStatement.php line 123: SQLSTATE[42804]: Datatype mismatch: 7 ERROR: column "company_id" cannot be cast automatically to type integer HINT: You might need to specify "USING company_id::integer". In PDOStatement.php line 121: SQLSTATE[42804]: Datatype mismatch: 7 ERROR: column "company_id" cannot be cast automatically to type integer HINT: You might need to specify "USING company_id::integer".

¿Cómo especificamos USING para cambiar esta columna de tipo cadena a entero en PostgreSQL?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Debe especificar una conversión explícita ya que no hay una conversión implícita (automática) de texto o varchar a entero. No conozco una función de Laravel para especificar el reparto, por lo que le sugiero que use una declaración DB sin procesar para lograr esto.

Podrías hacer esto:

 public function up() { DB::statement('ALTER TABLE job_listings ALTER COLUMN company_id TYPE integer USING (company_id)::integer'); }

También puede haber casos en los que haya espacios en blanco en su texto o campos varchar, por lo que tendría que recortar antes de lanzar

 public function up() { DB::statement('ALTER TABLE job_listings ALTER COLUMN company_id TYPE integer USING (trim(company_id))::integer'); }
over 4 years ago · Santiago Trujillo Report

0

Incluso las tablas tienen una fila o no, todavía devuelve ese error. Entonces, si no desea una consulta sin formato y su columna no tiene valor o tiene pero no es importante, simplemente suelte la columna y cree una nueva:

 public function up() { Schema::table('job_listings', function (Blueprint $table) { $table->dropColumn('company_id'); $table->integer('company_id'); }); }
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!