Loopback4 MySQL Auto-Increment ID - mysql

I've just installed Loopback4 (based on TS) and i'm trying to play around with it since it seems really easy to create an API using it. My question is rather simple (yet i dont know the answer). How can i increment the id of my model ?
Lets say i've got this in my model (created with lb4 model) ->
export class Post extends Entity {
#property({
type: 'number',
id: true,
required: false,
})
Id: number;
The first post (without adding ID) creates it with Id = 0. There's no next post since the Id doesnt auto-increment.
Any tip on how to do it? I'm using MySQL as stated in my title and LB4.
EDIT: Or even better, is there any way to modify the way Loopback4 (loopback-next) creates tables ? Theres one command that would need to be run after the DB tables have been created, something like ALTER TABLE post CHANGE Id Id INT(11) NOT NULL AUTO_INCREMENT;
I keep searching through the tree of options, but i've found nothing relevant and i dont think connecting to the db separately for this task is a good idea.

Fixed thanks to github. Apparently it is very similar to LB3 (which i didnt use).
#property({
type: 'number',
id: true,
generated: true,
})
Id: number;
Adding generated: true turns on the auto-increment on mysql.

Related

Manually setting annotation on generated migration using EF and MySql

I have a property that is in my class that is not the primary key that I want to auto increment. The primary key is a GUID so I can still use the auto increment function on another column in the table. Also I can't change the primary key to int as the GUID key is defined in a base class. I can manually add the .Annotation("MySQL:AutoIncrement", true) to the property in the generated migration but I'm concern about editing the migration causing future issues. I found what would be the answer via the .AddAnnotation(,) method but it doesn't created the desired results.
Also [DatabaseGenerated(DatabaseGeneratedOption.Identity)] doesn't produce the desired result.
I was hoping this:
builder.Entity<Editor>().Property(p => p.CreatorId).ValueGeneratedOnAdd().Metadata.AfterSaveBehavior = PropertySaveBehavior.Throw;
builder.Entity<Editor>().Property(p => p.CreatorId).Metadata.AddAnnotation("MySQL:AutoIncrement", true);
Would make this:
migrationBuilder.CreateTable(
name: "editor",
columns: table => new
{
CreatorId = table.Column<int>(nullable: false).Annotation("MySQL:AutoIncrement", true)
...
MySql.Data.EntityFrameworkCore: 8.0.18.0
Microsoft.EntityFrameworkCore: 2.2.4
After using DotPeek to look at the code for MySql.Data.EntityFramework it seems to not be possible to reach the desired affect with fluentApi or attributes as it will only add the annotation if it's the primary key. Regardless of if it's possible in the database.

SQL migrations. Keeping old ID is not working

I'm migrating an old database to a new database (mysql => postgres).
For simplicity i kept the old IDs so id did something like that in knex
knex("my_table").withSchema("mySchema").insert({
id : old[i].id,
info : old[i].info
})
It appears doing that cancels postgres auto_inc on ID and thus when i try to insert later like that :
knex("my_table").withSchema("mySchema").insert({
info : "information"
})
It will automaticly attribute it to ID : 1. Even if it already exist because of the migration.
My question is : Can i still keep the old id ?
please try altering the sequence assigned to the my_table.id column for auto-increment.
alter it such that it will start from max(old_id)+1
http://www.postgresqltutorial.com/postgresql-serial/
https://www.postgresql.org/docs/9.1/static/sql-altersequence.html

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,''

Sequelize create object with associations

I'm trying to save sequelize models with their associations. All the associations are one to one. Retrieving models with associations from the database works just fine but inserting them is another matter and the documentation is just making me more confused.
Here's my insert method:
models
.radcheck
.create(user, {
include: [{model: models.skraningar}, {model: models.radusergroup}, {model: models.radippool}]
})
.then(success, error);
I've seen so many ways to do this both in the documentation and here on stackoverflow and none of them make sense to me so far. Anyone care to clear things up for me?
I always save a model with association by it's foreign key
e.g
models.user.create({ name : 'test', usergroup_id : 1 });

Thinking Sphinx "no field found in schema" error

I am pretty new to Sphinx.
Trying to find user with name "bob" and company_id "14".
Controller:
#users = User.search 'bob', :conditions => { :company_id => '14'}
Model:
define_index do
indexes :name
indexes :company_id
end
Error:
index user_core: query error: no field 'company_id' found in schema
I have the 'company_id' in table & I re-indexed everything several times.
When I am just searching for the 'name' everything works properly.
Just as another helpful hint: turns out I had to change the way I called Model.search(), since my field was listed as an attribute (i.e. using has), I needed to call the search method with :with instead of :conditions (for fields).
Attributes should be declared as:
has company_id
So, in you case:
Model:
define_index do
indexes :name
has :company_id
end
And one more helpful hint, if you happen to be an idiot like me:
If you are getting this error and you are correctly using attributes and fields, it may be that you forgot to restart your dev server after adding a new field to the index.