Laravel: Unknown column type "timestamp" requested - mysql

I am trying to perform a migration and I am getting the following problem.
Unknown column type "timestamp" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgotten to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.
My code is the following:
Schema::table('XXXXXXXX', function (Blueprint $table) {
$table->timestamp('start')->change();
$table->timestamp('end')->change();
});
The strange thing is that I have already performed migrations with that type of data:
Schema::create('XXXXXX', function (Blueprint $table) {
...
$table->timestamp('date_expired')->nullable();
...
});
Does anyone know how to fix it or see the error I'm doing.
Thanks
UPDATE
In the end I have deleted the migration, I have modified it putting timestamp in the necessary columns and I have executed it again. (Having deleted the table before from the database)

On the laravel docs page you can find a warning telling you that there are certain types that you can't use with the ->change() method.
Link to laravel docs
It also says this:
To modify a timestamp column type a Doctrine type must be registered.

Related

Laravel5.5: migration string to JSON

How to make a migration in Laravel 5.5 that change the datatype of a String to JSON? My column has image links and I want to change it to JSON to store more links.
I thought this was the best way?
change the string value of the column so it corresponds to JSON datatype
change the datatype of the column to JSON
I'm able to do the first step in mysql by:
UPDATE db.vendor_horses SET image= CONCAT('{"images":["', image, '"]}');
Thanks!
You can easily create a new migration with php artisan make:migration migration_name command.
Use the following to change the datatype
Schema::table('vendor_horses', function (Blueprint $table) {
$table->json('image')->change();
});
The change() method allows you to set nullable, modify data length and types. Read more about change()
this just supported mySql version 8 to up
Schema::table('table_name', function (Blueprint $table) {
$table->json('field_name')->change();
});
First, If you use laravel and change your database table field then you install composer require doctrine/dbal Read more about modifying a column in laravel
Simply create a new migration file from php artisan make:migration migration_name and then open newly created migration file
Schema::table('your_table_name',function(Blueprint $table){
$table->json('image')->change();
//make nullable to image field
$table->json('image')->nullable()->change();
//rename image field to other
$table->renameColumn('old_column_name', 'new_column_name');
})
The change() method allow you to set data length , change your column name, and make nullable

FindBy columnName when column name contains "Id"

In Grails, Gorm, I have this entity:
class MyEntity implements Serializable {
Long bankTransactionId
int version
BigDecimal someValue
static constraints = {
bankTransactionId(nullable: false)
version(nullable: true)
someValue(nullable: true)
}
}
Doing MyEntity.findByBankTransactionId(Long.valueOf("3")) throws this exception:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown
column 'this_.id' in 'field list'
I am suspecting the fact that my column has the name id in it. Could it be this?
How to fix it then ?
Thanks.
Everything you have provided here looks fine. In particular, there are no restrictions about having the letters "id" in a column name.
Take a look at your generated MySQL table. I'm guessing that the id column isn't there for some reason. Maybe something prevented generating it due to some earlier error that you have now corrected, or you have your datasource set to "none" instead of "update" (or similar) and the whole table is missing!
If this is just a development environment with no real data (and no foreign key constraints), drop the whole MyEntity table and let it be automatically recreated. If not, move to a different temporary datasource, let a new table be created, and compare the two. If the new one still doesn't have an id column, you have something going wrong during your startup that is preventing your tables from being created correctly. You could just add the column in manually, but if you don't figure out what happened to it in the first place, it will probably just happen again.
For reference, in my test environment, my MySQL table for "MyEntity" copied from your example looks like:
desc my_entity;
'id','bigint(20)','NO','PRI',NULL,'auto_increment'
'version','int(11)','YES','',NULL,''
'bank_transaction_id','bigint(20)','NO','',NULL,''
'some_value','decimal(19,2)','YES','',NULL,''

when defining a table in SequelizeJs its name is changed when executing

when defining a table in SequelizeJs its name is changed when executing resulting in a ER_NO_SUCH_TABLE.
This is my code :
var API_TOKEN=database.sequelize.define('API_TOKENS',{
user_id:{
type:Sequelize.INTEGER,
},
token:{
type:Sequelize.STRING
}
});
and the error :
Unhandled rejection SequelizeDatabaseError: ER_NO_SUCH_TABLE: Table 'tableName.API_TOKENs' doesn't exist
Note:notice how the table name is changed when executed , i am using mysql database.
You can use one of two available options that can be put in the options of sequelize.define method
freezeTableName
tableName
According to the sequelize documentation (concerning how it names the database table basing on model definition)
By default, sequelize will automatically transform all passed model names (first parameter of define) into plural.
By using the freezeTableName, the database table will be named exactly the same as your model name. On the other hand, if you want fully custom table name, you should use the tableName attribute.

Unknown column type when generating schema

I am developing an application using Symfony2 and Doctrine, the problem comes when
I try to create tables for Role and User Entities using php app/console doctrine:schema:update --force, the relation is ManyToMany and although the mapping information, the annotations, seem to be correct it throws the following exception:
[Doctrine\DBAL\DBALException] Unknown column type role requested
What can I do? Thank you very much.
You probably have a annotation like this:
/**
* #Column(type="role")
*/
protected $role;
This is a problem, because Doctrine doesn't know of any type named role.
I have to say that this is an assumption because you haven't posted any code...

Problem with fieldname having '?'

I have a 'user' table with a field name 'process_salary?' which has a boolean datatype
#user = User.create(params[:user])
if #user.process_salary?
//some code here
else
//some code here
end
When I create a new object of user and check for process_salary it gives me following error
NoMethodError: undefined method `process_salary?' for #<User:0xb6ac2f68>
Why does this error occur? Can I avoid it without changing my column name?
When I check it with the debugger it crashes the first time, but after that it runs properly
The question-mark has a special meaning in ActiveRecord. It can be used to check whether a field is true. You are using it as part of your field name which wasn't such a good idea. You could try if #user.process_salary?? exists but I think ultimately it is easiest to change your database column to be called 'process_salary'.
Side note: The 'rails console' is really helpful for playing around with models.
As cellcortex posted, question marks at the end of column names are tricky in Rails. If you need to have it there for legacy reasons, you might be able access the attribute as follows:
#user['process_salary?']
or the more verbose:
#user.read_attribute['process_salary?']
You can of course test for nil using .nil?.