I have a question, what do i set the property type...
# box-products
o
I have a question, what do i set the property type for a foreign key? Example migration:
Copy code
component {

	function up( schema, query ){
		schema.create( "characters", function(table) {

			table.unsignedInteger( "userid" ).references( "id" ).onTable( "users" ).onDelete( "cascade" );
			...
		} );
	}

	function down( schema, query ){
		schema.drop( "characters" );
	}

}
😉 1
e
What do you mean the
property type
?
Do you mean the type of
users.id
?
Because you’d set that on your
users
table and it must match your
userid
column on the table with the foreign key.
o
@elpete I mean the property type for the model itself.
e
I’m sorry, I’m lost then. Where in your code is the property type?
r
@Ookma-Kyi are you using the quick ORM library?
o
@elpete Here is some code I whipped up very quickly:
Copy code
component extends="quick.models.BaseEntity" accessors="true" {

    property name="id" type="number";
    // foreign key to user.id
    property name="userid" type="nuber";
    property name="deleted" type="boolean";
    property name="active" type="boolean";
    property name="name" type="string";
    property name="xp" type="number";
    // foreign key to belt.id
    property name="belt" type="number";
    property name="wins" type="number";
    property name="loses" type="number";
    property name="draws" type="number";

    public Character function retrieveCharacterByName( required string name ){
        return newEntity().where( "name", arguments.name ).firstOrFail();
    }

    public Character function retrieveCharacterById( required numeric id ){
        return newEntity().findOrFail( arguments.id );
    }

    public struct function getMemento(){
        return { "name" : variables.getName() };
    }

}
@Ryan Albrecht Yes I am using quick ORM
r
If I understand your question correctly, the properties for a quick entity do not need a type specified,. The database values will be mapped to each of the properties regardless if it is a foreign key or not.
Looking at your entity you have not implemented the relations. Eg.
Copy code
component 
	extends="BaseEntity" 
	accessors="true" 
	table="currency_pair"
{

	property name="id";
	property name="code";
	property name="baseCurrencyId";
	property name="quoteCurrencyId";


	this.constraints.append({
		code: { required: true },
		baseCurrencyId: { required: true },
		quoteCurrencyId: { required: true },
	});


	function baseCurrency() {
		return belongsTo( "Currency",  'baseCurrencyId');
	}

	function quoteCurrency() {
		return belongsTo( "Currency", 'quoteCurrencyId' );
	}
}
you need to implement one of the relationship type ( hasOne, hasMany, belongsTo, belongsToMany etc etc). In my case I have two foreign keys.
baseCurrencyId
and
quoteCurrencyId
which are both many to one relationships
e
If you do need to specify a
sqltype
, you can add a
sqltype
attribute to your property. https://quick.ortusbooks.com/guide/getting-started/defining-an-entity#sql-type
Also, Quick and CFMigrations/CommandBox Migrations are good friends, but by no means required companions. I was initially confused by your question because you gave us a migration file and then proceeded to ask a Quick question.
👍 1
o
is:
Copy code
property name="code";

	this.constraints.append({
		code: { required: true },
	});
}
not the same as:
Copy code
property name="code" required="true";
r
@Ookma-Kyi no its not. I should have removed that bit for clarity sake and you can ignore. That bit of code is used for the cbValidation library.
I would highly recommend checking it out as it makes your life so much easier