question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expose unique constrains metadata to schema API and T4 model

See original GitHub issue

Hi, TT model generator sets correctly PrimaryKey attribute to properties of classes with attribute Table. Would it be possible to add also a new attribute, named AlternateKey(string alternateKeyName), to identify properties that partecipate to a unique constraint?

For example:

	[Table(Schema="DWUSER", Name="ST01_COMPONENT")]
	public partial class ST01Component
	{
		[Column("ID_COMPONENT"),     PrimaryKey,  NotNull] public decimal IdComponent     { get; set; } // NUMBER
		[Column("SITE"),             AlternateKey("ST01_NC_CODE_SITE_NC_CODE_UN"),  Nullable] public string  Site            { get; set; } // NVARCHAR2(18)
		[Column("SFC"),              AlternateKey("ST01_NC_CODE_SITE_NC_CODE_UN"),  Nullable] public string  Sfc             { get; set; } // NVARCHAR2(384)
		[Column("ITEM"),                Nullable         ] public string  Item            { get; set; } // NVARCHAR2(384)
		[Column("ITEM_DESCRIPTION"),    Nullable         ] public string  ItemDescription { get; set; } // NVARCHAR2(120)
}

The use case is the possibility to use this new attribute to generate new code based on it without the need to call the back end database another time.

Thank you!

Environment details

linq2db version: 3.0.0-preview.1 Database Server: Oracle Database Provider: Oracle.ManagedDataAccess.Core (3.0.0-preview.1) Operating system: Windows 10 .NET Framework:.NET Core 3.1

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
MaceWinducommented, Jan 15, 2020

You need to add code to your .tt file (not to linq2db t4 templates) between model load and generation steps https://linq2db.github.io/articles/T4.html#example-of-generation-process-customization

and then go through Tables collection and add additional attributes to table columns.

0reactions
sdanylivcommented, Jan 15, 2020

You are right, a lack of time to implement. You can help us to retrieve Unique constraints by SchemaProvider. It can be good first PR.

Anyway HasUniqueKey helps to define unique constraint for the subquery, when you know which data returned by subquery.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Defining Constraints and Indexes — SQLAlchemy 2.0 ...
Below we define a table invoice which has a composite primary key: invoice = Table( "invoice", metadata_obj, Column("invoice_id", Integer, primary_key=True) ...
Read more >
How to change the behaviour of unique true in django ...
You can use a UniqueConstraint [Django docs] with a condition instead of the unique kwarg on the field. Although there is a caveat...
Read more >
Constraints reference
... module create database constraints. They are added in the model Meta.constraints option. ... You must always specify a unique name for the...
Read more >
Entity framework attributes. In the Choose Model Contents step
Whether the index is unique. Entity Framework 6 provides the [Index] attribute to create an index on a particular column in the database....
Read more >
How do distributed databases follow unique constraints?
You need to choose some transactional model. It is key-value, SQL ACID transactions, Calvin transactions, something else? Different things will ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found