In migration, if we want to add a primary key on table, we use
$table->increments('id');
This will create an id field with int(10)
But I don't found any way to customize the primary key. Like what if I want primary key with string type or int with size 5 etc...
BTW, I want the primary key of tiny type. How can I achieve that using migration?
Thanks,
Parth Vora
You can customize Primary Key by using primary method as:
$table->tinyInteger('pk');
$table->primary('pk');
Docs
$table->increments('id'); this statement is just telling that you are auto increment id field , if you didn't explicitly define primary key it automatically consider id field as a primary key, you can define like this.
$table->primary('table_id');
as per documentation
Hope this will help.
Related
Our customer has given the access to views in which there is no primary key is defined. I know Entity Framework needs a primary key for table to identify.
But for views not having primary key is it still possible to query.
I try to find but always Entity Framework gives error saying:
Error: : EntityType 'ViewWeight' has no key defined. Define the key for this EntityType.
I understand key is important for tables, but for views just to read is there any hack or way to read the values without modifying the view itself.
It's not possible in Entity Framework to have Entities without primary key.
Try to get a possible unique key from the views, combining columns, ... to create a unique primary key.
If is not possible there is a workaround, if is only a queryable view, with out need to do other operations with retrieved values such delete or update. Modify the view to add NEWID() , it will generate a unique GUID ID for each row, use this new column as primary key for your entity.
CREATE VIEW FooView AS
SELECT SELECT NEWID() AS ID,
COLUMN_A,
COLUMN_B
.....
The problem is if you repeat the same query every time you will get different ID for the same row.
Updated
If you can't not modify the view you can use Entity with a raw Sql, create the raw sql as
List<MyView> myViewItems = context.MyView.SqlQuery("SELECT NEWID() AS ID, MyView.* FROM MyView").ToList();
In your models add
public Guid ID { get; set; }
And configure the new property as the primary key.
But be careful, because there is not compilation check with this kind of code.
I create the view which includes a primary key. Ensure that all fields in the view are of a specific data type:
Number(9) rather than Number, use CAST to get the type you want
Then add a disabled primary key constraint. It won't do anything except be recognized by entity framework as a key
alter view emp_view add constraint vemp_pk primary key (empno) disable
I have data table in the data base with the columnEmail set as nvarchar(100)(becuse i couldn't set it as a primary key when it was nvarchar(MAX).
so it is primary key now, but i cant change it Identity Specification to yes, so I cant make a relationship with this table and another table when this is the primary key.
How can i make a relationship when this is as the primary key?
How can i set the Identity Specification to yes? or is there another way without doing it?
Thanks in advanced
The concept of identity applies to an integer column. The database will automatically assign an increasing number to each new row. An identity column is typically a primary key.
So it makes no sense for a varchar column to be an identity column. SSMS is right in graying the identity section out.
A foreign key can refer to any data type, including a varchar(100). A foreign key has to be indexed. A column that is a primary key always has an index on it.
The foreign key column and the column it references must have the same data type. Perhaps you could post the definition of the two tables you are trying to link.
I have specific situation where composite primary key of one entity is part of the primary key of another entity. This is case of specialization, but it doesn't matter now.
I use Doctrine to generate entities from database, but Doctrine doesn't support composite foreign key as primary key:
It is not possible to map entity 'XXXXX' with a composite primary key as part of the primary key of another entity 'YYYYYY#id_xxxxx'
Does anyone know solution for this situation? It can be Doctrine solution or editing model and database structure.
UPDATE 1
CREATE TABLE `amandman` (
`iddokumenta` int(11) NOT NULL,
`datumdostavljanjaskupstini` date NOT NULL,
`tekst` text,
`datumizmene` date DEFAULT NULL,
`izmenjenitekst` text,
`iddokumentapredlogazakona` int(11) DEFAULT NULL,
`datumdostavljanjaskupstinipredlogazakona` date DEFAULT NULL,
PRIMARY KEY (`iddokumenta`,`datumdostavljanjaskupstini`),
KEY `iddokumentapredlogazakona_idx` (`iddokumentapredlogazakona`,`datumdostavljanjaskupstinipredlogazakona`),
CONSTRAINT `iddokumenta45` FOREIGN KEY (`iddokumenta`, `datumdostavljanjaskupstini`) REFERENCES `dokument` (`iddokument`, `datumdostavljanjaskupstini`) ON DELETE NO ACTION ON UPDATE CASCADE,
CONSTRAINT `iddokumentapredlogazakona` FOREIGN KEY (`iddokumentapredlogazakona`, `datumdostavljanjaskupstinipredlogazakona`) REFERENCES `predlogzakona` (`iddokumenta`, `datumdostavljanjaskupstini`) ON DELETE NO ACTION ON UPDATE CASCADE)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
This is one of entities from database that can't be generated by Doctrine.
You are running into this problem because your composite foreign key is another table's composite primary key. This is not a good development practice, which is why it is simply not supported by Doctrine, and I strongly doubt that it ever will be.
Solution 1 (preferred):
Add a single, auto-increment primary key to EstablecimientosSec. You can then link to that EstablecimientosSec.id instead.
Solution 2:
If changing the database structure is absolutely not possible, do not map the relationship. Instead, you can fetch the related EstablecimientosSec entities in a separate query using the composite primary key. It's not a prefect solution, but it works under these constraints. Tip: avoid querying the related objects as part of a loop.
The foreign key ever is a primary key in other table and why it's not a good practice?.
The solution is remove the foreign key relation from the database, after add it manually. Well if you are using cascade update or similar it's necesary or you can control the update/delete with code and not with relation of tables
My application needs a generic lookup table Dictionary that should only be referenced by KEY {VARCHAR(N) UNIQUE}
Is there any reason I should not set KEY to be the primary key?
You can definitely use a VARCHAR as a primary key. But being a primary key it would be difficult to update in case of any chance. But as per your need you can definitely use VARCHAR as primary key
is there anyway to create lets say pattern for primary key i.e. for table products such pattern would by p-1,p-2... p-n etc.
Thanks
Well, you can manually create and enforce that pattern into your application (or using triggers). A primary key just needs to be unique to work.
But I don't recommend it. In your sample, seems P-1 have a business meaning. And, if it belongs to your business realm, it can be changed. While most database have a UPDATE CASCADE equivalent, it doesn't change basic reason you shouldn't use that as key: it's information, not data.
I suggest you to create a field named ProductCode char(10) NOT NULL UNIQUE and maybe to fill it with P-00000001, P-00000002, and so on. Maybe you do prefer to use a varchar: this doesn't matter, as it must fulfill your business requirement. Create an Id INTEGER AUTO_INCREMENT PRIMARY KEY field to use as primary key, as it doesn't never needs to be changed.